doufanglian7585 2015-09-06 05:19
浏览 57
已采纳

使用Ajax访问stdClass对象

I'm sending an Ajax request to the database to retrieve all data from a particular MySQL table. I'm getting the result successfully. However, the result I'm getting is the stdClass object. Now I'm confused how to access them using jQuery to output them into my page.

// 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
});

And I'm getting the result like the following PHP stdClass object format:

// 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
)

So, I wish to know how you can actually access the result as an Javascipt object to output like: data.body

  • 写回答

4条回答 默认 最新

  • du970294 2015-09-06 05:33
    关注

    You will have to send your data from your php server to client as 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);
        }
    });
    

    Also remove return falsel line from main.js

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 修改光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解