admin管理员组

文章数量:1183193

I need to count the length of an Ajax response done in jQuery. The response is in JSON format and only contains a single string. I get the value but have no idea how to count the length of this string.

Here's my code :

var tempId;
$.ajax({
    url: "<?=base_url();?>index.php/sell/decoder",
    type: "POST",
    data: {'str' : sometext},
    dataType: 'json',
    async: false,
    success: function(response) {
        tempId = response; // This gives me a return value as a string. For example = 153
        alert(tempId.length); // But this returns "undefined". What should I do to get the length?
    }
});

Here's the structure of the response header:

Connection  Keep-Alive 
Content-Length  2
Content-Type    text/html
Date    Fri, 06 Jul 2012 08:12:12 GMT
Keep-Alive  timeout=5, max=86
Server  Apache
X-Powered-By    PHP/5.3.10

I need to count the length of an Ajax response done in jQuery. The response is in JSON format and only contains a single string. I get the value but have no idea how to count the length of this string.

Here's my code :

var tempId;
$.ajax({
    url: "<?=base_url();?>index.php/sell/decoder",
    type: "POST",
    data: {'str' : sometext},
    dataType: 'json',
    async: false,
    success: function(response) {
        tempId = response; // This gives me a return value as a string. For example = 153
        alert(tempId.length); // But this returns "undefined". What should I do to get the length?
    }
});

Here's the structure of the response header:

Connection  Keep-Alive 
Content-Length  2
Content-Type    text/html
Date    Fri, 06 Jul 2012 08:12:12 GMT
Keep-Alive  timeout=5, max=86
Server  Apache
X-Powered-By    PHP/5.3.10
Share Improve this question edited May 16, 2014 at 21:26 DanM7 2,2463 gold badges29 silver badges47 bronze badges asked Jul 6, 2012 at 7:57 under5hellunder5hell 9973 gold badges17 silver badges40 bronze badges 9
  • 2 Can you show the structure of the response – Teneff Commented Jul 6, 2012 at 7:59
  • If you could alert(tempId.length); why not alert(tempId); too, also you could use console.log(tempId) to check it in console. – xdazz Commented Jul 6, 2012 at 8:01
  • Do a console.log(response) and show us the result. And, why are you using async: false? Don't do that, because synchronous requests may temporarily lock the browser, disabling any actions while the request is active. – Angel Commented Jul 6, 2012 at 8:06
  • Possible duplicate of: stackoverflow.com/questions/3334341/… – mayrs Commented Jul 6, 2012 at 8:07
  • 1 Do a console.log(response) as the first line of the success function. And show the output as is. I say as is because you said the response is a string, but in your comment you say its 153, which is a integer. "153" would be the string. It may have been a typo, which is why am asking for the response as is. – Amith George Commented Jul 6, 2012 at 8:08
 |  Show 4 more comments

4 Answers 4

Reset to default 15

Do an if condition then convert it to string first, then count the length as needed.

success: function(response) {
    if(response){       
      alert( (response + '').length );
    }
}

Or convert your value (I guess it is an integer) to string:

tempId.toString().length

tempId.String.length worked for me!

If you know the response is not an object then

success: function(response) {
    if(response){       
      alert( (response + '').length );
    }
}

will work well.

but if response will be in the form of Object like

[{name:'some-name',details:[....something]}]

I would suggest use below code

success: function(response) {
     length=0;
     if(response){       
        length=JSON.stringify(response).length;
     }
    console.log(length)
}

I think this code will work like a charm for you.

本文标签: javascriptGet the length of jQquery Ajax ResponseStack Overflow