Get the length of jQquery Ajax Response

前端 未结 4 795
你的背包
你的背包 2021-01-17 17:19

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 coun

相关标签:
4条回答
  • 2021-01-17 18:07

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

    success: function(response) {
        if(response){       
          alert( (response + '').length );
        }
    }
    
    0 讨论(0)
  • 2021-01-17 18:16

    tempId.String.length worked for me!

    0 讨论(0)
  • 2021-01-17 18:20

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

    tempId.toString().length
    
    0 讨论(0)
  • 2021-01-17 18:20

    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.

    0 讨论(0)
提交回复
热议问题