admin管理员组

文章数量:1278881

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 e json.length is not 3???

UPDATE: So I changed my asp 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?

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 e json.length is not 3???

UPDATE: So I changed my asp 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?

Share Improve this question edited Apr 22, 2011 at 19:35 Stellar Sword asked Apr 22, 2011 at 19:20 Stellar SwordStellar Sword 6,21618 gold badges77 silver badges102 bronze badges 4
  • Are you sure there are no cross-domain problems? – js1568 Commented Apr 22, 2011 at 19:23
  • Nope, I'm testing this on localhost and the server is running on localhost IIS7. The url used is "../check.ashx" (also I can see the server return '99', so obviously it's working.) – Stellar Sword Commented Apr 22, 2011 at 19:23
  • if (!json.length) is true if json.length = 0, undefined or blank. And you want to alert json[0] and json.length? That will fail if length is undefined – mplungjan Commented Apr 22, 2011 at 19:25
  • See, json.length should NOT be 0, undefined, or blank, because obviously json has data. So in my code above, json[0] has data... but json.length is undefined. Not possible. Obviously something went wrong. If json[0] has data, json.length should be at least 1. This code has been used this way in other example codes too so it shouldn't be returning true for !json.length. – Stellar Sword Commented Apr 22, 2011 at 19:30
Add a ment  | 

3 Answers 3

Reset to default 8

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:

var count = 0;
for (var foo in bar) {
    if (bar.hasOwnProperty(foo) {
        count++;
    }
}

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./

You can convert your object to JSON using :

var jsonVariable= JSON.stringify(objectVariable);
var jsonVariableLength = jsonVariable.length ;

And print the length :

alert('Length : ' + jsonVariableLength );

本文标签: javascriptjQuery ajax json response has length undefined and incorrect dataStack Overflow