使用Ajax访问stdClass对象

时间:2015-09-06 05:19:25

标签: php jquery mysql ajax

我向数据库发送Ajax请求以检索特定MySQL表中的所有数据。我成功地获得了结果。但是,我得到的结果是stdClass对象。现在我很困惑如何使用jQuery将它们输出到我的页面中来访问它们。

// fetch.php

// Display particular SMS by sid
$result = $database->get_by_sid($_POST['sid']);

// Rendering the result to Ajax
foreach ( $result as $message ) {
    print_r($message);
}
// main.js
$.ajax({
    url: '/admin/fetch.php',
    data: 'sid=' + sid,
    method: 'POST',
    beforeSend: function () {
        $('.progress').show();
    },
    complete: function () {
        $('.progress').hide();
    },
    success: function (data) {
        console.log(data);
    },
    error: function () {
        Materialize.toast('Cannot send the request. Please try again later or refresh the page', 4000);
    }
return falsel
});

我得到的结果类似于以下PHP stdClass对象格式:

// console
stdClass Object
(
    [id] => 1
    [user_name] => admin
    [phone_number] => +16469267421
    [from] => +16469267421
    [body] => Hi Ngrok
    [sid] => SM1fe8a7a5a65261a7ebc765e020b1d89a
    [time] => 2015-09-03 06:18:13
    [status] => received
)
stdClass Object
(
    [id] => 2
    [user_name] => admin
    [phone_number] => +16469267421
    [from] => +16469267421
    [body] => Okay. so the request is perfect :)
    [sid] => SM4b4d9fc928e12e3ddbf63df6204fb4cd
    [time] => 2015-09-03 06:20:19
    [status] => received
)
stdClass Object
(
    [id] => 3
    [user_name] => admin
    [phone_number] => +16469267421
    [from] => +16469267421
    [body] => Wow, I'm really happy to see this now :)
    [sid] => SM4704dff6517c02e284cbfe812730917c
    [time] => 2015-09-03 06:21:06
    [status] => received
)
stdClass Object
(
    [id] => 4
    [user_name] => admin
    [phone_number] => +16469267421
    [from] => +16469267421
    [body] => I hope everything is now working correctly as expected.
    [sid] => SM6577ca7e346551486d2572acfe99aec0
    [time] => 2015-09-03 06:32:22
    [status] => received
)
stdClass Object
(
    [id] => 5
    [user_name] => admin
    [phone_number] => +16469267421
    [from] => +16469267421
    [body] => Hi, Good morning :)
    [sid] => SM7f3d9adbfd1274c58efaaeb9a3aca013
    [time] => 2015-09-03 07:02:30
    [status] => received
)

所以,我想知道如何实际访问结果作为Javascipt对象输出如:data.body

4 个答案:

答案 0 :(得分:1)

您可以先获取对象,然后创建一个数组

while($row= mysqli_fetch_row($result))
{
   $arr[]= $row;
}
echo json_encode($arr);
exit;

它会在json中给出回应。如果你想以数组格式获得响应,你可以这样做。

  print_r($arr);
  exit;

答案 1 :(得分:0)

首先,在服务器端构建一个结果数组:

$output = [];
foreach ($result as $message) {
    $output[] = $message;
}

然后,当您准备将其发送回javascript时,设置内容类型:application / json标头并打印数组的json编码版本:

header('Content-Type: application/json');
echo json_encode($output);
exit;

答案 2 :(得分:0)

您必须将数据从php服务器发送到客户端作为JSON

header('Content-Type: application/json');
$result = $database->get_by_sid($_POST['sid']);

// Rendering the result to Ajax
$data = [];
foreach ( $result as $message ) {
    $data[] = $message;
}
echo json_encode($data);
exit;

// main.js
$.ajax({
    url: '/admin/fetch.php',
    data: 'sid=' + sid,
    method: 'POST',
    beforeSend: function () {
        $('.progress').show();
    },
    complete: function () {
        $('.progress').hide();
    },
    success: function (data) {
        $.each ( data, function ( i, v ) {
            //v.id id
            //v.user_name user_name
            //v.phone_number phone_number
        });
    },
    error: function () {
        Materialize.toast('Cannot send the request. Please try again later or refresh the page', 4000);
    }
});
  

同时从 return falsel

中删除 main.js

答案 3 :(得分:0)

您是如何从数据库中获取数据的?如果将它们作为assoc Array获取,则可以使用json_encode。

在你的fetch.php(而不是foreach)中:

echo json_encode($result);