【问题标题】:Returning a JSON object from PHP in AJAX call?在 AJAX 调用中从 PHP 返回 JSON 对象?
【发布时间】:2012-01-28 19:04:35
【问题描述】:

这是我在 jQuery AJAX 调用期间调用的 PHP 代码:

<?php
    include '../code_files/conn.php';
    $conn = new Connection();
    $query = 'SELECT Address_1, Address_2, City, State, OfficePhone1, OfficePhone2, Fax1, Fax2, Email_1, Email_2 
              FROM clients WHERE ID = ?';
    $conn->mysqli->stmt_init();
    $stmt = $conn->mysqli->prepare($query);
    $stmt->bind_param('s', $_POST['ID']);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    echo json_encode($row);
?>

而客户端代码是:

$.post(url, {ID:$('#ddlClients').val()},
        function(Result){
            // Result
        }
      );

AJAX 调用已成功完成。我将 Result 的值设为

"{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road",.....and so on

我想要的是能够使用返回的值,如 Result.Address_1、Result.Address_2 等。但我不能使用上面的代码来做到这一点。我尝试使用$row = $result-&gt;fetch_object()$row = $result-&gt;fetch_array(),但没有用。

而且我知道这可以通过服务器端的代码来完成:

$row = $result->fetch_assoc();
$retVal = array("Address_1"=>$row['Address_1'], "Address_2"=>$row['Address_2'].......);
echo json_encode($retVal);

$row = $result->fetch_object();
$retVal = array("Address_1"=>$row->Address_1, "Address_2"=>$row->Address_2.......);
echo json_encode($retVal);

有没有办法将 $row 直接发送到客户端 JavaScript 并准备用作 JSON 对象,而无需先手动创建数组?

【问题讨论】:

标签: php jquery json


【解决方案1】:

您从 PHP 脚本获得的响应是​​纯文本。但是,您可以在回调函数中使用 $.parseJSON 将该字符串解析为对象:

$.ajax({
    url      : url,//note that this is setting the `url` property to the value of the `url` variable
    data     : {ID:$('#ddlClients').val()},
    type     : 'post',
    success  : function(Result){
            var myObj = $.parseJSON(Result);
            //you can now access data like this:
            //myObj.Address_1
        }
    }
  );

您可以通过将 AJAX 调用的 dataType 属性设置为 json 来让 jQuery 为您执行此操作:

$.ajax({
    url      : url//note that this is setting the `url` property to the value of the `url` variable
    data     : {ID:$('#ddlClients').val()},
    dataType : 'json',
    type     : 'post',
    success  : function(Result){
            //you can now access data like this:
            //Result.Address_1
        }
    }
  );

上述示例期望来自服务器的响应采用这种格式(来自您的问题):

"{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road"}

【讨论】:

  • 是的。响应格式就像您提到的那样。但我没有解析它。
【解决方案2】:

在您的$.post 调用中,最后一个参数可能是数据类型:json:

$.post(url, {ID:$('#ddlClients').val()},
    function(Result){
        alert(Result.Address_1);
    },'json'
 );

然后一切都应该正常工作,因为看起来你做的一切都是正确的。

【讨论】:

  • 谢谢。那行得通。我假设 $.post() 会自动转换来自服务器的响应。
【解决方案3】:

json_encode 接受对象,因此无需进行自动数组构建。:

$row = $result->fetch_object();
echo json_encode($row);

就这么简单!

【讨论】:

  • 你可以看到我的问题。我已经这样做了。我缺少的是在进行 AJAX 调用时没有指定类型“json”。
  • 不,您手动创建了数组,所以我回答了“有没有办法将 $row 直接发送到客户端 JavaScript 并准备用作 JSON 对象,没有先手动创建数组?"
  • 抱歉,请看我问题中的第一段代码。我也试过你提到的。两种方式,我都必须在 jQuery 中解析响应。无论如何,谢谢。
  • 那么你的问题就被打破了,因为带问号的句子最终显然不是你真正需要帮助的东西......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多