为什么array.length返回错误的数字?

Bra*_*Prb 3 javascript php arrays

这是我的javascript代码,我每隔5秒调用一个php文件,php文件以javascript数组的形式返回一些id,但是array.length给了我18,而数组中只有2个元素.

PHP文件响应:

["20","1"]
Run Code Online (Sandbox Code Playgroud)
//function to rotate thumbnails
  window.setInterval(function () {    
            var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var index;
                var a = xmlhttp.responseText;
                for (index = 0; index < a.length; index++) {
                    pass_value_to_other_function(a[index]);
                }               
            }
        }
        xmlhttp.open("GET", "thumb_rotator.php", true);
        xmlhttp.send();
    }, 5000);
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要解析对javascript对象/数组的响应.

var a = JSON.parse(xmlhttp.responseText);
Run Code Online (Sandbox Code Playgroud)