无法从JQuery ajax调用接收JSON

Jay*_*ett 10 ajax jquery json

我已经确定来自服务器的JSON是有效的(手动进行ajax调用),但我真的很想使用JQuery.我还确定使用firebug发送到服务器的"post"URL是正确的.但是,仍然会触发错误回调(解析错误).我也尝试过数据类型: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
Run Code Online (Sandbox Code Playgroud)

Ada*_*ber 19

以下是我会尝试的一些建议:

1)您指定的'datatype'选项应为'dataType'(我认为区分大小写)

2)尝试使用'contentType'选项,如下所示:

contentType: "application/json; charset=utf-8"
Run Code Online (Sandbox Code Playgroud)

我不确定在你的帖子网址的请求中使用它会有多大帮助,而不是在响应中.有关详细信息,请参阅此文章:http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax (它是为asp.net编写的,但可能适用)

3)三重检查你的帖子的输出并通过JSON验证器运行输出只是为了绝对确定它是有效的并且可以解析为JSON对象.http://www.jsonlint.com

希望有些帮助!

  • 只需注意,将dataType设置为"json"会自动将contentType设置为"application/json". (7认同)