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
DanM7
  • 2,203
  • 3
  • 28
  • 46
under5hell
  • 997
  • 3
  • 16
  • 40
  • 2
    Can you show the structure of the response – Teneff Jul 06 '12 at 07: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 Jul 06 '12 at 08: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 Jul 06 '12 at 08:06
  • Possible duplicate of: http://stackoverflow.com/questions/3334341/jquery-javascript-json-to-string-variable-dump – mayrs Jul 06 '12 at 08:07
  • @MohammadAdil you are definitely right. thanks. I never noticed that thing :-) But, is there another way to get the length of the value contained in Json object? – under5hell Jul 06 '12 at 08: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 Jul 06 '12 at 08:08
  • 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` – under5hell Jul 06 '12 at 08:13
  • @Angel I do that to solve the previous problem I met. I need to put the response value into variable tempId in order to use it outside the ajax success. If I turn on the async and call the tempId, all i get is undefined. – under5hell Jul 06 '12 at 08:17
  • True, but you should move the logic to the success function of the ajax, or have a function call in the success, to take advantage of the async of ajax. – Angel Jul 06 '12 at 08:50

4 Answers4

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 );
    }
}
fedmich
  • 5,343
  • 3
  • 37
  • 52
8

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

tempId.toString().length
Keyul
  • 729
  • 1
  • 8
  • 20
Niranda
  • 79
  • 4
0

tempId.String.length worked for me!

Surekha
  • 582
  • 1
  • 7
  • 12
0

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.