Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .length计算字符数,而不是数组长度_Javascript_Php_Arrays - Fatal编程技术网

Javascript .length计算字符数,而不是数组长度

Javascript .length计算字符数,而不是数组长度,javascript,php,arrays,Javascript,Php,Arrays,我想计算json返回的数组的项目数。当我使用response.length时,它会计算数组中的所有字符,而我希望它做的是计算数组中有多少项。同样的方法也适用于其他页面,但不适用于此页面 这是php代码: ...$response[] = array("id" => $row['appid'], "hour" => $row['hour'], "tna

我想计算json返回的数组的项目数。当我使用response.length时,它会计算数组中的所有字符,而我希望它做的是计算数组中有多少项。同样的方法也适用于其他页面,但不适用于此页面

这是php代码:

            ...$response[] = array("id" => $row['appid'],
                            "hour" => $row['hour'],
                            "tname" => $tname,
                            "tsurname" => $tsurname,
                            "cname" => $cname,
                            "csurname" => $csurname,
                            "cgsm" => $cgsm,
                            "cemail" => $cemail,
                            "cdept" => $cdept,
                            "cdeg" => $cdeg,
                            "purpose" => $purpose,
                            "clientnotshown" => $row['clientnotshown']);


    };

    if(isset($response)){

    echo json_encode($response);

    } else {

    $response = array("val" => 0);
    echo json_encode($response);

    };
Javascript代码:

function updateTable() {

var getData = {
                      date: $("#displaydate").val(),
                      operation:"getData"
                  }

                  $.post( "printoperations.php", getData).done(function( response ) {
                      if (response.val != 0){

                          alert("so far so good")
                          var arrayLength = response.length
                          alert(response)
                          alert(arrayLength)}
};
这是我得到的一张照片。我想得到物品的数量,在本例中是2。

您需要解析响应var response=JSON.parseresponse,然后response.length将返回所需的结果。

您需要解析响应var response=JSON.parseresponse,然后response.length将返回所需的结果。

从PHP返回的任何内容都是字符串。
var getData = {
                  date: $("#displaydate").val(),
                  operation:"getData"
              }

              $.post( "printoperations.php", getData).done(function( response ) {
                  if (response.val != 0){

                      alert("so far so good")
                      var responseArray = JSON.parse(response)
                      alert(responseArray)
                      alert(responseArray.length)}
};
但是,如果您发送正确的头,或者在设置中告诉jQuery它是JSON,jQuery将自动解析该字符串

$.post("printoperations.php", getData, function(response) {
    if (response.val != 0) {
        console.log("so far so good")
        var arrayLength = response.length
        console.log(response)
        console.log(arrayLength)
    }
}, 'json'); // <- here

并使用控制台调试

从PHP返回的任何内容都始终是字符串。 但是,如果您发送正确的头,或者在设置中告诉jQuery它是JSON,jQuery将自动解析该字符串

$.post("printoperations.php", getData, function(response) {
    if (response.val != 0) {
        console.log("so far so good")
        var arrayLength = response.length
        console.log(response)
        console.log(arrayLength)
    }
}, 'json'); // <- here

并使用控制台进行调试

似乎您正在尝试查找字符串的长度。相反,尝试alerttypeof response,如果它给出字符串,则在执行任何操作之前,使用JSON.parseresponse将其解析为JSON,然后重试。

似乎您正在尝试查找字符串的长度。相反,尝试alerttypeof response(如果它给出字符串),然后在执行任何操作之前,使用JSON.parseresponse将其解析为JSON,然后重试。

JSON.parseresponse成功。谢谢你的回答

JSON.parseresponseThat成功了。谢谢你的回答

你应该把这个

标题'Content-Type:application/json'

在服务器端php查询中。

您应该将

标题'Content-Type:application/json'


在您的服务器端php查询中。

解析该对象的响应,然后获取其计数。您是否尝试解析JSON以获取实际数组?或者在调用$时指定“json”作为数据类型。post和jQuery将为您解析它。json.parseresponse,或者告诉jQuery您需要一个json响应。很抱歉,我不知道该怎么做。将该响应解析为对象,然后计数。您是否尝试解析json以获得实际数组?或者在调用$时指定“json”作为数据类型。post和jQuery将为您解析它。json.parseresponse,或者告诉jQuery您需要一个json响应。很抱歉这么无知,但我不知道怎么做。