javascript jQuery ajax json 响应的长度未定义且数据不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5759150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax json response has length undefined and incorrect data
提问by Dexter
I'm trying to grab a dictionary object which is converted to json object in server side, (along with correct content-type header), but for some reason, even though I can access part of the data, other parts don't show up and json object in jquery has length equal to 0.
我正在尝试获取一个字典对象,该对象在服务器端转换为 json 对象(以及正确的内容类型标头),但出于某种原因,即使我可以访问部分数据,其他部分也没有显示jquery 中的 up 和 json 对象的长度等于 0。
Here is my jquery call:
这是我的 jquery 调用:
$.ajax({
type : "POST",
url : cl._url,
//data : 'text='+text,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType : "json",
error : function(XHR, status, error) {
alert("There was an error processing the request.\n\n"+XHR.responseText);
},
success : function(json){
if (!json.length) {
alert('There are no incorrectly spelled words...'+json[0]+ ' ' + json.length);
} else {
// highlight bad words
}
cl.$container.html(html);
// execute callback function, if any
(callback != undefined) && callback();
}
});
I usually get the alert box with this code, and json[0] prints out 99 as expected. But json.length is "undefined". So in a sense, the json returned is right but my code will not detect it and use it.
我通常会收到带有此代码的警报框,并且 json[0] 会按预期打印出 99。但是 json.length 是“未定义的”。所以从某种意义上说,返回的 json 是正确的,但我的代码不会检测到它并使用它。
When I go directly to my ashx page where my json data is printed on the screen, I get this json object:
当我直接进入我的 json 数据打印在屏幕上的 ashx 页面时,我得到了这个 json 对象:
{"id":1,"json":[5,10,15,20],"0":"99"}
Just a sample json output. So how come json.length is not 3???
只是一个示例 json 输出。那么 json.length 怎么不是 3 ???
UPDATE: So I changed my asp.net code, from Dictionary to List, and then added the same values. And suddenly, length is functioning now. ?!?!?! So objects don't have lengths in javascript?
更新:所以我将我的 asp.net 代码从字典更改为列表,然后添加了相同的值。突然之间,长度现在起作用了。?!?!?!所以对象在javascript中没有长度?
回答by Quentin
Objects don't have a length property unless you give them one. Arrays do, but arrays are created with []
not {}
.
对象没有长度属性,除非你给它们一个。数组可以,但数组是用[]
not创建的{}
。
If you want to know how many properties an object has, you have to loop over them and count them:
如果你想知道一个对象有多少个属性,你必须遍历它们并计算它们:
var count = 0;
for (var foo in bar) {
if (bar.hasOwnProperty(foo) {
count++;
}
}
回答by 3coins
Best way to debug this is to inspect the response under Firebug console. See if you are even getting back a valid response.
调试此问题的最佳方法是检查 Firebug 控制台下的响应。看看你是否甚至得到了有效的回应。
回答by Jad Chahine
You can convert your object to JSON
using :
您可以将对象转换为JSON
使用:
var jsonVariable= JSON.stringify(objectVariable);
var jsonVariableLength = jsonVariable.length ;
And print the length :
并打印长度:
alert('Length : ' + jsonVariableLength );