无法从 JQuery ajax 调用接收 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/79498/
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
Unable to receive JSON from JQuery ajax call
提问by Jay Corbett
I have determined that my JSON, coming from the server, is valid (making the ajax call manually), but I would really like to use JQuery. I have also determined that the "post" URL, being sent to the server, is correct, using firebug. However, the error callback is still being triggered (parse error). I also tried datatype: text.
我已经确定我的来自服务器的 JSON 是有效的(手动进行 ajax 调用),但我真的很想使用 JQuery。我还使用 firebug 确定发送到服务器的“发布”URL 是正确的。但是,错误回调仍然被触发(解析错误)。我也试过数据类型:文本。
Are there other options that I should include?
我应该包括其他选项吗?
$(function() {
$("#submit").bind("click", function() {
$.ajax({
type: "post",
url: "http://myServer/cgi-bin/broker" ,
datatype: "json",
data: {'start' : start,'end' : end},
error: function(request,error){
alert(error);
},
success: function(request) {
alert(request.length);
}
}); // End ajax
}); // End bind
}); // End eventlistener
回答by Adam Weber
Here are a few suggestions I would try:
以下是我会尝试的一些建议:
1) the 'datatype' option you have specified should be 'dataType' (case-sensitive I believe)
1)您指定的“数据类型”选项应该是“数据类型”(我相信区分大小写)
2) try using the 'contentType' option as so:
2) 尝试使用 'contentType' 选项,如下所示:
contentType: "application/json; charset=utf-8"
I'm not sure how much that will help as it's used in the request to your post url, not in the response. See this article for more info: http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax(It's written for asp.net, but may be applicable)
我不确定这会有多大帮助,因为它用于对您的帖子 url 的请求,而不是在响应中。有关更多信息,请参阅这篇文章:http: //encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax(它是为 asp.net 编写的,但可能适用)
3) Triple check the output of your post url and run the output through a JSON validator just to be absolutely sure it's valid and can be parsed into a JSON object. http://www.jsonlint.com
3) 三重检查您的帖子 url 的输出并通过 JSON 验证器运行输出,以确保它是有效的并且可以解析为 JSON 对象。http://www.jsonlint.com
Hope some of this helps!
希望有些帮助!
回答by Bohdan Hdal
Why myResult
instead of request
?
为什么myResult
而不是request
?
success: function(request) {
alert(myResult.length);
}
回答by Big Al
The data parameter is wrong. Here is an example that works:
数据参数错误。这是一个有效的示例:
data: { index: ddl.selectedIndex },
数据:{索引:ddl.selectedIndex},
This contructs an object with property called index with value ddl.selectedIndex.
这将构造一个名为 index 且值为 ddl.selectedIndex 的属性的对象。
You need to remove the quotes from your data parameter line
您需要从数据参数行中删除引号
Good luck A
祝你好运