使用 jsonp 错误的 jQuery ajax 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5247295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax request using jsonp error
提问by brafales
I'm writing an app and I need to access some json data in the client side from another server. Because of the cross domain issue I plan on using jsonp. jQuery allows me to do this using the $.getJSON() method, however, I have no way to tell if the method has failed (i.e., the server is not responding or something). So I tried the approach to get the JSON data using $.ajax instead. But it's not working and I don't know what to try. Here an example showing my issue:
我正在编写一个应用程序,我需要从另一台服务器访问客户端中的一些 json 数据。由于跨域问题,我计划使用 jsonp。jQuery 允许我使用 $.getJSON() 方法执行此操作,但是,我无法判断该方法是否失败(即,服务器没有响应或其他原因)。所以我尝试了使用 $.ajax 获取 JSON 数据的方法。但它不起作用,我不知道该尝试什么。这是一个显示我的问题的示例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>TEST</title>
<script type="text/javascript" src="scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#button_detect').click(function(){
var feedApiAjax = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=';
var feedApiGetJSON = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=';
var feedUrl = 'http://www.engadget.com/rss.xml';
$.ajax({
url: feedApiAjax + feedUrl,
datatype: 'jsonp',
success: function(data) {
console.log('$.ajax() success');
},
error: function(xhr, testStatus, error) {
console.log('$.ajax() error');
}
});
$.getJSON(
feedApiGetJSON + feedUrl,
function(data) {
console.log('$.getJSON success');
});
});
});
</script>
</head>
<body>
<div id="button_detect">CLICK ME!!!!</div>
</body>
If you create a web page with this code and click on the "Click Me" div you'll see that the $.getJSON request is working and the $.Ajax one is not. I've tried putting/removing the "callback=?" tg, used "jsonp" and "json" data types, but non of this worked.
如果您使用此代码创建一个网页并单击“单击我”div,您将看到 $.getJSON 请求有效,而 $.Ajax 请求无效。我试过放置/删除“回调=?” tg,使用“jsonp”和“json”数据类型,但这些都不起作用。
Any idea on what might I be doing wrong?
知道我可能做错了什么吗?
Cheers!
干杯!
采纳答案by Ahmed Atia
I encountered this error before, and solved after using jquery-JSONP
之前遇到过这个错误,使用jquery-JSONP后解决
[Example]
[例子]
$.getJSON('http://server-url/Handler.ashx/?Callback=DocumentReadStatus',
{
userID: vuserID,
documentID: vdocumentID
},
function(result) {
if (result.readStatus == '1') {
alert("ACCEPTED");
}
else if (result.readStatus == '0') {
alert("NOT ACCEPTED");
}
else {
alert(result.readStatus);
}
});
回答by djp3
When the dataType is jsonp, jquery won't fire the error function automatically. This is described in the documentation under the "error" option:
当 dataType 为 jsonp 时,jquery 不会自动触发错误函数。这在“错误”选项下的文档中有所描述:
Note: This handler is not called for cross-domain script and JSONP requests. see here.
注意:跨域脚本和 JSONP 请求不会调用此处理程序。 看到这里。
The workaround is to explicitly specify a timeout as an option. If the server doesn't respond in the specified amount of time then the error function will be called. More about this here.
解决方法是明确指定超时作为选项。如果服务器在指定的时间内没有响应,则将调用错误函数。更多关于这里。
回答by dc2009
for parsing external feeds, and catching possible errors, you can use this
为了解析外部提要并捕获可能的错误,您可以使用它
function GetContent(feedUrl)
{
var feedApiGetJSON = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=';
$.ajax({
url: feedApiGetJSON + feedUrl,
dataType: 'jsonp',
jsonpCallback: 'JsonpCallback'
});
}
function JsonpCallback(data) {
if (data.responseStatus == "200")
alert(data.responseData.feed.title);
else
alert(data.responseDetails);
回答by Ela Giza
Replace: datatype with dataType
替换:数据类型与数据类型