简体   繁体   English

无法从 JQuery ajax 调用接收 JSON

[英]Unable to receive JSON from JQuery ajax call

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.我已经确定我的来自服务器的 JSON 是有效的(手动进行 ajax 调用),但我真的很想使用 JQuery。 I have also determined that the "post" URL, being sent to the server, is correct, using firebug.我还使用 firebug 确定发送到服务器的“发布”URL 是正确的。 However, the error callback is still being triggered (parse error).但是,错误回调仍然被触发(解析错误)。 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) 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.我不确定这会有多大帮助,因为它用于对您的帖子 url 的请求,而不是在响应中。 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)有关更多信息,请参阅这篇文章: 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. 3) 三重检查您的帖子 url 的输出并通过 JSON 验证器运行输出,以确保它是有效的并且可以解析为 JSON 对象。 http://www.jsonlint.com http://www.jsonlint.com

Hope some of this helps!希望有些帮助!

Why myResult instead of request ?为什么是myResult而不是request

success: function(request) {
  alert(myResult.length);
}

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祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM