javascript - 请求的资源上不存在 Access-Control-Allow-Origin header

标签 javascript jquery ajax http get

DISCLAIMER: This question is a question about question. So that makes this question a meta question. It does not have any connection to the previously asked questions. If you find any resemblance, lemme tell you one thing- it's purely coincidental.

我想从我的网页发出 AJAX 请求。我一直在尝试这样做,但没有一种方法能完美运行。我发现的唯一贴近现实的帖子是this .

我尝试了来自 SO 和其他类似网站的各种其他方法,但所有这些帖子对我来说只说了一件事。

"No 'Access-Control-Allow-Origin' header is present on the requested resource."

我知道您现在会将此问题标记为重复,因为有很多类似的问题。现在..让我告诉你一件事。我尝试了我在 SO 中找到的每一 block sh*t,但没有一个给了我我正在寻找的结果。并不是因为他们都错了。这是因为我对如何使用它们一无所知。最后……我选择了上面提供的链接。这很容易……但我需要了解有关代码的某些事情。这是我第一次听到漂亮的首字母缩略词——CORS。所以,如果有人能帮助我理解我提出的问题,请为你们所有人投票。我想在今年第三次庆祝我的生日之前解决这个狗娘养的问题。我会以资源和问题的形式告诉你我所拥有的一切。

1) A rotten server located at Elizabeth town.
2) I need to access it.
3) I am planning to make a HTTP GET request.
4) I have a url. (eg. http://whereismyweed.com)
5) I store it into a JavaScript variable. var stoner='http://whereismyweed.com'
6) I have a HTML div tag in my webpage. (<div id="myprecious"></div>)
7) I wanna display the response I get from the server inside of 'myprecious' div.
8) And last but not the least... my AJAX function. (Courtesy: some website I visited)




$.ajax({
                url: stoner,
                    data: myData,
                    type: 'GET',
                    crossDomain: true, // enable this
                    dataType: 'jsonp',
                    success: function() { alert("Success"); },
                error: function() { alert('Failed!'); },
                beforeSend: setHeader
            });

什么是“我的数据”??它包含什么。我怎样才能得到这个请求的回应?什么是“setHeader”?有什么讲究吗???如何在 myprecious div 中显示响应?我应该对该功能进行哪些更改?这个函数正确吗?

问题太多了吧????好吧...我只需要一个通用答案吗?

最佳答案

您的功能是正确的。请按照以下步骤实现您的目标-

 //for getting response modify your code like 

      success:function(response){
            alert(response);
            $('#myprecious').html(response); //myprecious is id of div
     }

  // myData variable is jSon object which contains request parameter has to send.Eg.
     var myData = {'first_name':'Foo','last_name':'Bar'}  // now on server first_name and last_name treated as request parameter.


  // If server not required any special headers to validate request 'setHeader' does not require. by default $.ajax will take care of it. you can remove it.



     /// final code looks like 


         $.ajax({
            url: stoner,
                data: myData, 
                type: 'GET',
                crossDomain: true, // enable this
                dataType: 'jsonp',
                success: function(response ) { $('#myprecious').html(response);    
  },
                error: function() { alert('Failed!'); }

        });

关于javascript - 请求的资源上不存在 Access-Control-Allow-Origin header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658940/

相关文章:

jquery - 如何在 joomla 中使用 jQuery.post()

javascript - 如何使用 Jquery 获取包括空格的整个字符串?

ajax - 如何使用 Ajax 使用 VS 2012 Internet 模板更新 RenderBody() 部分?

javascript - 使用 JavaScript、jQuery 和 AJAX 避免达到 CPU 限制的正确方法是什么?

javascript - javascript中递归循环返回未定义的数字

javascript - jQuery AJAX 数据类型 : 'script' not working on firefox

jquery - 从 iframe 中重新加载父窗口

php - 图像原始数据的强制下载在 android 2.3 上损坏

javascript - ExtJS 6 - 我们应该在哪里编写样式?

javascript - 使用 Bootstrap 工具提示和 jQuery 进行验证?