jQuery ajax json 响应的长度未定义且数据不正确
我试图获取一个在服务器端转换为 json 对象的字典对象(以及正确的内容类型标头),但由于某种原因,即使我可以访问部分数据,其他部分也不会显示up 和 jquery 中的 json 对象的长度等于 0。
这是我的 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();
}
});
我通常会使用此代码收到警报框,并且 json[0] 按预期打印出 99。但 json.length 是“未定义”。所以从某种意义上说,返回的 json 是正确的,但我的代码不会检测到它并使用它。
当我直接进入 ashx 页面(其中我的 json 数据打印在屏幕上)时,我得到这个 json 对象:
{"id":1,"json":[5,10,15,20],"0":"99"}
只是一个示例 json 输出。那么为什么 json.length 不是 3???
更新:所以我更改了我的 asp.net 代码,从字典更改为列表,然后添加相同的值。突然间,长度开始发挥作用了。 ?!?!?!那么 JavaScript 中的对象没有长度?
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.
Here is my jquery call:
$.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.
When I go directly to my ashx page where my json data is printed on the screen, I get this json object:
{"id":1,"json":[5,10,15,20],"0":"99"}
Just a sample json output. So how come json.length is not 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对象没有长度属性,除非你给它们一个。数组可以,但数组是使用
[]
创建的,而不是{}
。如果你想知道一个对象有多少个属性,你必须循环它们并计算它们:
Objects don't have a length property unless you give them one. Arrays do, but arrays are created with
[]
not{}
.If you want to know how many properties an object has, you have to loop over them and count them:
调试此问题的最佳方法是检查 Firebug 控制台下的响应。看看您是否能得到有效的回复。
http://getfirebug.com/
Best way to debug this is to inspect the response under Firebug console. See if you are even getting back a valid response.
http://getfirebug.com/
您可以使用以下方法将对象转换为
JSON
:并打印长度:
You can convert your object to
JSON
using :And print the length :