我需要在jQuery中计算Ajax响应的长度。响应采用JSON格式,只包含一个字符串。我可以获取该字符串的值,但不知道如何计算该字符串的长度。
这是我的代码:
这是我的代码:
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?
}
});
这是响应头的结构:
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
alert(tempId.length);
,为什么不也用alert(tempId);
呢?同时你也可以使用console.log(tempId)
在控制台中查看它。 - xdazz