10

jQuery で行われた Ajax 応答の長さをカウントする必要があります。応答は JSON 形式で、1 つの文字列のみが含まれます。値を取得しましたが、この文字列の長さを数える方法がわかりません。

これが私のコードです:

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
4

4 に答える 4

15

最初に if 条件を実行し、それを文字列に変換してから、必要に応じて長さを数えます。

success: function(response) {
    if(response){       
      alert( (response + '').length );
    }
}
于 2012-07-06T08:12:22.223 に答える
8

または、値(整数だと思います)を文字列に変換します:

tempId.toString().length
于 2012-07-06T08:37:48.107 に答える
0

tempId.String.length私のために働いた!

于 2012-12-31T20:06:15.287 に答える