javascript “长度”为空还是不是对象?浏览器 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3284429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:44:37  来源:igfitidea点击:

'length' is null or not an object? IE 8

javascriptjqueryinternet-explorer

提问by Scarface

Hey guys quick question I get the following error 'length is null or not an object' in IE 8, anyone have any ideas? Feedback greatly appreciated...

嘿伙计们快速问题我在 IE 8 中收到以下错误“长度为空或不是对象”,有人有任何想法吗?反馈非常感谢...

 function refresh() {
$.getJSON(files+"handler.php?action=view&load=update&time="+lastTimeInterval+"&username="+username+"&topic_id="+topic_id+"&t=" + (new Date()), function(json) {
    if(json.length) {
      for(i=0; i < json.length; i++) {
        $('#list').prepend(prepare(json[i]));
        $('#list-' + count).fadeIn(1500);
      }
      var j = i-1;
      lastTimeInterval = json[j].timestamp;
    }
  });

}

回答by Pointy

Just check for the object being null or empty:

只需检查对象是否为 null 或为空:

if (json && json.length) {
  // ...
}

C'mon gang this was glaringly obvious :-)

来吧,这太明显了:-)

回答by rfunduk

JSON objects (returned by jQuery or otherwise) do not have a lengthproperty. You'll need to iterate over the properties, most likely, or know the structure and simply pull out what you want:

JSON 对象(由 jQuery 或其他方式返回)没有length属性。您最有可能需要遍历属性,或者了解结构并简单地提取出您想要的内容:

$.getJSON( ..., ..., function( json ) {
  for( var prop in json ) {
    if( !json.hasOwnProperty( prop ) ) { return; }
    alert( prop + " : " + json[prop] );
  }
} );

Alternatively, grab a library like json2and you'll be able to stringify the object for output/debugging.

或者,获取像json2这样的库,您将能够将对象字符串化以进行输出/调试。

回答by Mark Schultheiss

pop the JSON in a span then clip it and paste it here so we can see it:

在 span 中弹出 JSON,然后将其剪辑并粘贴到此处,以便我们可以看到它:

<span id="JSObject2">empty</span>

with the json2.js from here: (link for it at bottom of the page) http://www.json.org/js.html

使用这里的 json2.js:(页面底部的链接)http://www.json.org/js.html

myJSON = JSON.stringify(json);
$('#JSObject2').text(myJSON);

Using that, we can help you better, and you can see what you have!

使用它,我们可以更好地帮助您,您可以看到您拥有什么!

回答by meder omuraliev

A JSON is an object, you seem to be treating it like an array. Does it really have a lengthproperty? Show us the JSON?

JSON 是一个对象,您似乎将其视为数组。它真的length有财产吗?向我们展示 JSON?

You might need to use a for..ininstead.

您可能需要使用 afor..in代替。

EDIT: Can you make the JSON from the backend structure like so?

编辑:你能像这样从后端结构制作 JSON 吗?

({
    "foo": [] 
})

回答by Matt Huggins

What does your returned JSON look like? If you're returning an object, length might not be explicitly defined, whereas if you're returning an array, it should be defined automatically.

你返回的 JSON 是什么样的?如果您要返回一个对象,则可能未显式定义长度,而如果您要返回一个数组,则应自动定义它。

回答by George

First thing that comes to mind is that length is not a property of whatever json is. What is the json variable supposed to be anyway?

首先想到的是长度不是任何 json 的属性。json 变量应该是什么?