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
|
Show 4 more comments
4 Answers
Reset to default 15Do 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
版权声明:本文标题:javascript - Get the length of jQquery Ajax Response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738281332a2072739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
alert(tempId.length);
why notalert(tempId);
too, also you could useconsole.log(tempId)
to check it in console. – xdazz Commented Jul 6, 2012 at 8:01