Get the length of jQquery Ajax Response

ajax response array length
check size of ajax response
ajax response count
how to get length of string using jquery
how to get length of input field in jquery
jquery length of string
response.length javascript
data length jquery

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 );
    }
}

jQuery Ajax Success get returned Array Length?, Then the Second Alert() to detect length response.length is returning: undefined. Then how can i loop this Array (or) how to loop the Fruits (  jQuery AJAX Intro jQuery Load jQuery Get/Post jQuery Misc The length property contains the number of elements in the jQuery object. Syntax $(selector).length

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

tempId.toString().length

jQuery, If you use Chrome, press F12 to open the development tools, and choose 'Network', you get an overview of requests, including AJAX requests. For each request there's a byte size of both the response content and the entire response. In FireBug you should be able to see similar data. I wouldn't suggest returning data in the response header. If you must return a count param, why not append it to your json? Or, if your json is returning an array of data, why not just get the array.length in javascript? – Steven Moseley Nov 26 '12 at 4:57

tempId.String.length worked for me!

Get the size of an AJAX response before it finishes loading Quick , Get the size of an AJAX response before it finishes loading. jQuery Ajax JavaScript Content length. Dcaa3d24e533fd5e19ec011294c1feea?d=  jQuery is a Javascript library which has been made to ease the development of JavaScript based websites and it offers massive framework for frontend web development. One major and maybe the best part of it is Ajax, Asynchronous JavaScript and XML. In common language you can you can load data into a website without refreshing it.

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.

.length, There are 2 divs.Click to add more. Ajax · Global Ajax Event Handlers  Get the length of jQquery Ajax Response. Ask Question Asked 7 years, 10 months ago. Active 7 months ago. Viewed 37k times 10. I need to count the length of an Ajax

.size(), Description: Return the number of elements in the jQuery object. version added: 1.0.size(). This method does not accept any arguments. $.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler. For more information on JSONP, see the original post detailing its use. Sending Data to the Server. By default, Ajax requests are sent using the GET HTTP method.

jQuery length Property, JavaScript Certificate · SQL Certificate · Python Certificate · jQuery Certificate · PHP Certificate · Bootstrap Certificate · XML Certificate · Get Certified »  I am using AJAX to get some data and fill-up the form. Data is quite large so it takes some time to get from DB and fill up in fields, so while all doing this stuff i am showing loading icon. 283

Can't get a JSON object in response to an Ajax request with wp_ajax , BODA82's answer helped, but eventually I realized that I should have replaced responseText with responseJSON method in my JavaScript code. In the example​  Since jQuery 3.x, $.ajax returns Promises - which is a browser-builtin technology to manage asynchronous code. If you’re going to use async/await, you don’t need to use jQuery.

Comments
  • Can you show the structure of the response
  • If you could alert(tempId.length); why not alert(tempId); too, also you could use console.log(tempId) to check it in console.
  • 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.
  • Possible duplicate of: stackoverflow.com/questions/3334341/…
  • 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.
  • this is a really nice solution for my problem sir. Thanks :-)
  • this is more elegant solution. But I noticed you have a typo there :-). thank you
  • yeah, .toString() is the func
  • tempId.toString().length will work only when the tempID is not a type of object. [{name:'test'}].toString() will return [object Object]. and [{name:'test'}].toString().length will return 15