[英]jQuery .ajax() calls failing
I'm trying to make a jQuery .ajax()
call to a public web service, and I'm having trouble finding the right syntax. 我正在尝试对公共Web服务进行jQuery
.ajax()
调用,但我无法找到正确的语法。
I've tried several different implementations. 我尝试了几种不同的实现方式。 This one:
这个:
$.ajax({
url: 'http://www.geognos.com/api/en/countries/info/all.jsonp',
dataType: "jsonp",
success: function() {
alert('JSONP call succeeded!');
}
});
It fails with the following error: 它失败并出现以下错误:
all.jsonp:1 Uncaught ReferenceError: callback is not defined
And this one: 还有这个:
$.ajax({
url: 'http://www.geognos.com/api/en/countries/info/all.json',
dataType: "json",
success: function() {
alert('JSON call succeeded!');
}
});
Fails with this error: 失败,出现此错误:
XMLHttpRequest cannot load http://www.geognos.com/api/en/countries/info/all.json. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.
I'm serving the page through my local IIS7 instance. 我通过本地IIS7实例提供页面服务。 I've also tried various combinations of
$.getJSON()
with similar results. 我也尝试过类似
$.getJSON()
各种组合。 What am I missing? 我错过了什么?
Here's a JSFiddle of the above code. 这是上面代码的JSFiddle 。
UPDATE: Thought we had a solution, but I'm still getting the callback is not defined
error when doing the JSONP calls, even though the alert/log code gets called. 更新:以为我们有一个解决方案,但是在执行JSONP调用时,我仍然得到
callback is not defined
错误,即使调用了警报/日志代码。 The response URL looks like this: 响应URL如下所示:
http://www.geognos.com/api/en/countries/info/all.jsonp?callback=undefined&157148585
and the JSON response is wrapped like this: 并且JSON响应包含如下:
callback({"StatusMsg": "OK", "Results": {"BD": {"Name": "Bangladesh", "Capital": {"DLST": "null", "TD": 6.0, "Flg": 2, "Name": "Dhaka", ...
I've found examples with the callback name added on to the end of the URL in the .ajax()
configuration, but when I try that I get the same result, only it's tacked on to the end of my query string. 我已经找到了在
.ajax()
配置中将URL添加到URL末尾的示例,但是当我尝试得到相同的结果时,只有它被添加到我的查询字符串的末尾。
This regular JSON call will not work because of same origin policy . 由于相同的原始策略,此常规JSON调用将不起作用。 This is what your error is telling you with:
is not allowed by Access-Control-Allow-Origin
. 这是您的错误告诉您的:
is not allowed by Access-Control-Allow-Origin
。
The correct JSONP syntax is: 正确的JSONP语法是:
$.ajax({
url: 'http://www.geognos.com/api/en/countries/info/all.jsonp',
dataType: "jsonp",
jsonpCallback: 'callback',
success: function(data) {
console.log(data);
}
});
The correct usage is buried in the documentation for $.ajax() . 正确的用法隐藏在$ .ajax()的文档中。 Search for the
jsonpCallback
option. 搜索
jsonpCallback
选项。
$.ajax({
url: 'http://www.geognos.com/api/en/countries/info/all.jsonp',
dataType: "jsonp",
jsonpCallback: function() {
alert('JSONP call succeeded!');
}
});
Fiddle: http://jsfiddle.net/gya3b/3/ 小提琴: http : //jsfiddle.net/gya3b/3/
You can make it work if you create a proxy to load the url for you. 如果您创建代理来为您加载网址,则可以使其正常工作。
$.ajax({
url: 'proxy.php?url=http://www.geognos.com/api/en/countries/info/all.json',
dataType: "json",
success: function() {
alert('JSON call succeeded!');
}
});
Here proxy.php
will load http://www.geognos.com/api/en/countries/info/all.json
for you. 在这里,
proxy.php
将为您加载http://www.geognos.com/api/en/countries/info/all.json
。
About the JSONP part, your syntax is invalid. 关于JSONP部分,您的语法无效。 See http://api.jquery.com/jQuery.ajax/ for more.
有关更多信息,请参见http://api.jquery.com/jQuery.ajax/ 。
Here is an example of how you can fix this. 以下是如何解决此问题的示例。 By setting your jsoncallback.
通过设置你的jsoncallback。
$.ajax(url, { dataType: 'jsonp', jsonp: 'jsoncallback' })
.then(function(data, status, xhr) {
console.log(status);
console.log('success (promises): ' + data.name);
}, function(xhr, status, error) {
console.log('failed (promises): ' + error);
});
If you need to make a web service call to a domain that is not of the same origin (ie base URL) you need to use a proxy to do so. 如果您需要对不同源的域(即基本URL)进行Web服务调用,则需要使用代理来执行此操作。 proxies are not necessarily obligated to the same domain restrictions.
代理不一定有义务使用相同的域名限制。
They are different depending on the platform you are working with, ie .NET/LAMP. 它们根据您使用的平台(即.NET / LAMP)而有所不同。
This website has multiple posts on how to create either. 该网站有多篇关于如何创建的帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.