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/