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

Swa*_*esh 3 php ajax jquery json

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

$.ajax({
  type: "POST",
  data: "qid=162",
  url: "activity_ajax.php",
  dataType: json,
  success: function (data) {
    alert(data.first);
  }
});
Run Code Online (Sandbox Code Playgroud)

我的activity_ajax.php页面返回以下内容

echo "first":"Steven","last":"Spielberg","address":"1234 Unlisted Drive";
Run Code Online (Sandbox Code Playgroud)

diE*_*cho 10

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

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

echo json_encode($output,JSON_FORCE_OBJECT);
Run Code Online (Sandbox Code Playgroud)

另一方面,您可以通过这种方式访问​​该值

 success : function(resp) {(
              alert(resp.first);
              alert(resp.last);
              alert(resp.address);
            });
Run Code Online (Sandbox Code Playgroud)