无法从JQuery ajax调用接收JSON
我已经确定来自服务器的JSON是有效的(手动进行ajax调用),但我真的很想使用JQuery。 我还确定使用firebug发送到服务器的“post”url是正确的。 但是,仍然会触发错误回调(parsererror)。 我也尝试过数据类型:text。
我还应该包括其他选项吗?
$(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
以下是我会尝试的一些建议:
1)您指定的’datatype’选项应为’dataType’(我认为区分大小写)
2)尝试使用’contentType’选项,如下所示:
contentType: "application/json; charset=utf-8"
我不确定在你的posturl的请求中使用它会有多大帮助,而不是在响应中。 有关详细信息,请参阅此文章: http : //encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax (它是为asp.net编写的,但可能适用)
3)三重检查你的post的输出并通过JSONvalidation器运行输出只是为了绝对确定它是有效的并且可以解析为JSON对象。 http://www.jsonlint.com
希望有些帮助!
为什么myResult
而不是request
?
success: function(request) { alert(myResult.length); }
数据参数错误。 这是一个有效的例子:
data:{index:ddl.selectedIndex},
这将使用值为ddl.selectedIndex的名为index的属性构造一个对象。
您需要从数据参数行中删除引号
祝你好运A.