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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With