Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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
使用php将多个值返回到带有json的jquery.ajax_Php_Jquery_Ajax_Json - Fatal编程技术网

使用php将多个值返回到带有json的jquery.ajax

使用php将多个值返回到带有json的jquery.ajax,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我正在尝试使用php和ajax从数据库中对数据进行简单调用。我需要多个结果。因此,我使用json方法。但它不起作用 $.ajax({ type: "POST", data: "qid=162", url: "activity_ajax.php", dataType: json, success: function (data) { alert(data.first); } }); My activity_ajax.php页面返回以下内容 echo "first"

我正在尝试使用php和ajax从数据库中对数据进行简单调用。我需要多个结果。因此,我使用json方法。但它不起作用

$.ajax({
  type: "POST",
  data: "qid=162",
  url: "activity_ajax.php",
  dataType: json,
  success: function (data) {
    alert(data.first);
  }
});
My activity_ajax.php页面返回以下内容

echo "first":"Steven","last":"Spielberg","address":"1234 Unlisted Drive";

您可以在一个数组中发送多个数据,然后使用

$output =  array('first'=>'Steven',
                 'last'=>'Spielberg',
                 'address'=>'1234 Unlisted Drive');

echo json_encode($output,JSON_FORCE_OBJECT);
另一方面,您可以通过这种方式访问值

 success : function(resp) {(
              alert(resp.first);
              alert(resp.last);
              alert(resp.address);
            });

您没有返回有效的JSON。。。将您的PHP更改为:

$temp = array('first' => 'Steven', 'last' => 'Spielberg', 'address' => '1234 Unlisted Drive');
echo json_encode($temp);
它将返回有效的JSON


该方法从各种源返回有效的JSON(关联数组就是其中之一)

。它解决了这个问题。我在数据类型:“json”参数中缺少了这些“”