Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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 使用PHP JSON encode将数据发送到jQuery getJSON的奇怪行为_Javascript_Php_Jquery_Json - Fatal编程技术网

Javascript 使用PHP JSON encode将数据发送到jQuery getJSON的奇怪行为

Javascript 使用PHP JSON encode将数据发送到jQuery getJSON的奇怪行为,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我正在将数据从PHP发送到jQuery。一切正常,除了我想从数据库发送总行数 我已经剥去了很多其他东西来解决这个根本问题 PHP代码: header('Content-type: text/json'); header('Content-type: application/json'); $rows = array with the data; That is working fine $total=mysql_num_rows($RSList); $total_data=array("tot

我正在将数据从PHP发送到jQuery。一切正常,除了我想从数据库发送总行数

我已经剥去了很多其他东西来解决这个根本问题

PHP代码:

header('Content-type: text/json');
header('Content-type: application/json');
$rows = array with the data; That is working fine

$total=mysql_num_rows($RSList);
$total_data=array("total"=>$total);
array_push($data,$total_data,$rows); 
echo json_encode($data);
jQuery代码:

$.getJSON("..url..", function( data ) {
  var data_array = eval(data);
  $.each(data_array[0], function(i, item ) {
    console.log(item)
  });
});
$.getJSON('...url...', function(data){
    console.log(data.length);
}
浏览器中显示的结果是:

[{"total":532},[{"product_id":"1",.... }]]
[{"total":100},[{"product_id":"1",.... }]]
[{"total":"(532)"},[{"product_id":"1",.... }]]
532是mysql\u num\u rows()中的正确值

控制台: 空字符串

我尝试了以下方法:

如果我在PHP中手动设置total的值:

$total=100;
浏览器中显示的结果是:

[{"total":532},[{"product_id":"1",.... }]]
[{"total":100},[{"product_id":"1",.... }]]
[{"total":"(532)"},[{"product_id":"1",.... }]]
java控制台: 一百

如果我在PHP中转换字符串中的值:

$total="(".$totalRows_RSList.")";
浏览器中显示的结果是:

[{"total":532},[{"product_id":"1",.... }]]
[{"total":100},[{"product_id":"1",.... }]]
[{"total":"(532)"},[{"product_id":"1",.... }]]
控制台: ()

我也尝试过
intval
number\u格式
strval
,但没有成功

非常非常奇怪还是

更新: 我已经按照建议修改了代码,但仍然存在相同的问题:

PHP:

脚本:

$.getJSON( "..url..", function( data ) {
    console.log( data )
    $.each( data.rows, function(i, item ) {...}
})
浏览器:

{"total":532,"rows":[{"product_id":"567",...}]}
控制台:

Object { rows=[99], total=""}

注意:这个答案解释了代码的错误。从中学习,然后看看@ojovirtual的答案,它显示了我认为对你来说,实现你最初打算做的事情的最佳方式结束注释

从代码中删除
eval
语句。jQuery已经解析了传入的JSON(如果它是有效的),您的success函数将以
data
param的形式接收您的数组。只需使用
数据
参数,而不是
数据数组

$.getJSON('..url..', function (data) {
    // if your PHP is sending proper JSON in the format you describe, `data` is now
    // an array containing two items. The first (`data[0]`) is an object which has
    // one property, `total`. The second (`data[1]`) is your array of row data, each
    // of which is an object.

    // your code in modified form, though I don't understand why you would use `each`
    $.each(data[0], function (i, item) {
        console.log(item);
    });

    // just use:
    console.log(data[0].total);
});

请务必阅读上述代码中的注释。

注意:此答案解释了代码的错误。从中学习,然后看看@ojovirtual的答案,它显示了我认为对你来说,实现你最初打算做的事情的最佳方式结束注释

从代码中删除
eval
语句。jQuery已经解析了传入的JSON(如果它是有效的),您的success函数将以
data
param的形式接收您的数组。只需使用
数据
参数,而不是
数据数组

$.getJSON('..url..', function (data) {
    // if your PHP is sending proper JSON in the format you describe, `data` is now
    // an array containing two items. The first (`data[0]`) is an object which has
    // one property, `total`. The second (`data[1]`) is your array of row data, each
    // of which is an object.

    // your code in modified form, though I don't understand why you would use `each`
    $.each(data[0], function (i, item) {
        console.log(item);
    });

    // just use:
    console.log(data[0].total);
});

请务必阅读上述代码中的注释。

您不需要传递总行数,只需获取已解析JSON对象的长度即可

jQuery代码:

$.getJSON("..url..", function( data ) {
  var data_array = eval(data);
  $.each(data_array[0], function(i, item ) {
    console.log(item)
  });
});
$.getJSON('...url...', function(data){
    console.log(data.length);
}
PHP代码:

header('Content-type: text/json');
header('Content-type: application/json');
$rows = array_with_the_data; //That is working fine
echo json_encode($rows);

您不需要传递总行数,只需获取已解析JSON对象的长度

jQuery代码:

$.getJSON("..url..", function( data ) {
  var data_array = eval(data);
  $.each(data_array[0], function(i, item ) {
    console.log(item)
  });
});
$.getJSON('...url...', function(data){
    console.log(data.length);
}
PHP代码:

header('Content-type: text/json');
header('Content-type: application/json');
$rows = array_with_the_data; //That is working fine
echo json_encode($rows);

如果在数组中使用键,它的外观和可读性会更好<代码>json_编码(数组('total'=>$total,'rows'=>$rows))和
$。每个(data.rows,…)
我已经更改了它,仍然是相同的问题。如果在数组中使用键,它的外观和可读性会更好<代码>json_编码(数组('total'=>$total,'rows'=>$rows))和
$。每个(data.rows,…)
我已经更改了,仍然是相同的问题。这是一个非常好的观点。OP应该使用我的答案来理解为什么他自己的代码不起作用,并且应该使用这个答案来实现他的目标。是的,我可以这样做,但我想在页面中传递更多数据,我从my_sql查询中得到的数据也有同样的问题。这是一个非常好的观点。OP应该使用我的答案来理解为什么他自己的代码不起作用,并且应该使用这个答案来实现他的目标。是的,我可以这样做,但是我想在页面中传递更多的数据,我从my_sql查询中得到的数据也有同样的问题。问题一定来自mysql_num_行($RSList),因为当我设置值total=99时,它正在工作,最奇怪的是在浏览器中,值很好[{“total”:532},[{“product_id”:“1”,…}]],问题一定是来自mysql_num_行($RSList),因为当我设置值total=99时,它正在工作,最奇怪的是在浏览器中,值很好[{“total”:532}[{“产品id”:“1”,…}]]