使用php将多个值返回到带有json的jquery.ajax
我正在尝试使用php和ajax从数据库中对数据进行简单调用。我需要多个结果。因此,我使用json方法。但它不起作用使用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"
$.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”参数中缺少了这些“”