无法从JQuery Ajax调用接收JSON

时间:2020-03-05 18:57:48  来源:igfitidea点击:

我已经确定来自服务器的JSON是有效的(手动进行ajax调用),但是我真的很想使用JQuery。我还使用Firebug确定了发送到服务器的"发布" URL是正确的。但是,错误回调仍在被触发(parsererror)。我也尝试过数据类型:文本。

我还有其他选择吗?

$(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)我们指定的"数据类型"选项应为"数据类型"(我相信是区分大小写的)

2)尝试按以下方式使用" contentType"选项:

contentType: "application/json; charset=utf-8"

我不确定这对发帖网址(而不是响应)的要求有多大帮助。
请参阅本文以获取更多信息: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

希望有些帮助!