获取jQquery Ajax响应的长度 [英] Get the length of jQquery Ajax Response

查看:206
本文介绍了获取jQquery Ajax响应的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个Ajax响应jQuery中所做的长度。响应是JSON格式,只包含一个字符串。我得到的价值,但不知道怎么算这个字符串的长度。

下面是我的code:

  VAR tempId;
$阿贾克斯({
    网址:,?< = BASE_URL();;&GT的index.php /销售/日codeR?
    键入:POST,
    数据:{str的:sometext},
    数据类型:JSON,
    异步:假的,
    成功:函数(响应){
        tempId =响应; //这给了我一个返回值作为字符串。例如= 153
        警报(tempId.length); //但是这将返回未定义。我应该怎么做才能长?
    }
});
 

下面是响应报头的结构:

 连接保持
内容长度2
内容类型text / html
日期星期五,2012年7月6日8点12分12秒GMT
保持活动超时= 5,最大值= 86
服务器阿帕奇
的X技术,通过PHP / 5.3.10
 

解决方案

做的,如果条件首先然后将其转换为字符串,然后计算长度根据需要。

 成功:函数(响应){
    如果(响应){
      警报((响应+'').length);
    }
}
 

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

这篇关于获取jQquery Ajax响应的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆