无法从JQuery的AJAX调用接收JSONJQuery、AJAX、JSON

2023-09-10 16:39:09 作者:抽风中、请勿扰

我已经决定了我的JSON,来自服务器,是有效的(手工制作Ajax调用),但我真的想使用jQuery。我也确定,后的网址,被发送到服务器,是正确的,使用萤火虫。但是,错误回调仍然被触发(parsererror)。我也试过数据类型:文本

是否还有其他的选择,我应该包括哪些内容?

  $(函数(){
    $(#提交)。绑定(点击,函数(){
        $阿贾克斯({
            类型:后,
            网址:HTTP:// MYSERVER / cgi-bin目录/经纪人,
            数据类型:JSON,
            数据:{启动:开始,结束:结束},
            错误:函数(请求错误){
                警报(错误);
            },
            成功:函数(要求){
                警报(request.length);
            }
        }); //结束阿贾克斯
    }); //结束绑定
}); //结束事件监听器
 

解决方案

下面是一些建议,我会尝试:

1)已指定要的dataType的数据类型选项(区分大小写的,我相信)

2)尽量使用'的contentType'选项,因为这样:

 的contentType:应用/ JSON的;字符集= UTF-8
 
ajax type是get请求,jQuery的Ajax请求数据时type无法使用GET

我不知道有多少,这将帮助,因为它是用在要求你的帖子的网址,而不是在响应中。 看到这篇文章的详细信息: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

希望一些这方面的帮助!

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 (parsererror). I also tried datatype: text.

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

解决方案

Here are a few suggestions I would try:

1) the 'datatype' option you have specified should be 'dataType' (case-sensitive I believe)

2) try using the 'contentType' option as so:

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)

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

Hope some of this helps!