如何使用 AJAX GET 请求来显示获取的 stdclass 对象

Posted

技术标签:

【中文标题】如何使用 AJAX GET 请求来显示获取的 stdclass 对象【英文标题】:how can you use an AJAX GET request to display fetched stdclass Objects 【发布时间】:2015-09-02 03:03:00 【问题描述】:

我已成功使用PDO 从我的数据库中获取数据,使用PDO::FETCH_OBJ 方法,查询返回如下:

Array (
       [0] => stdClass Object
       (
        [id] => 2
        [title] => Johnny Sims
        [img] => 
        [description] => 
        [school] => 
        [location] => 1
        [url] => 
        [tablename] => 2
        [votes] => 5
        [name] => President Candidates
        [Numopinions] => 6
    )

[1] => stdClass Object
    (
        [id] => 2
        [title] => Jeremy Pinto
        [img] => 
        [description] => 
        [school] => 
        [location] => 1
        [url] => 
        [tablename] => 2
        [votes] => 4
        [name] => Presidential Candidates
        [Numopinions] => 6
    )

[2] => stdClass Object
    (
        [id] => 2
        [title] => Sarah Bushels
        [img] => 
        [description] => 
        [school] => 
        [location] => 1
        [url] => 
        [tablename] => 2
        [votes] => 3
        [name] => Presidential Candidates
        [Numopinions] => 6
    )
    )

如果这是回显的内容,人们将如何使用 AJAX GET 请求与此数据进行交互?

【问题讨论】:

您可以像在没有AJAX 的情况下访问它一样访问它。什么意思? 唯一认为您可能需要做不同的是echo 使用jSON 编码。 【参考方案1】:

我假设你想在 javascript 中与它交互,在这种情况下你可以json_encode($array) 它然后将它输出到内容类型标头设置为 application/json 的页面。

如果您随后使用 AJAX 请求该页面并将结果加载到 var 中,它将自动解析为 JavaScript 数组。

【讨论】:

【参考方案2】:

这只是您的场景的表示。

index.php

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<!-- jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<div id="result"></div>

<script>
    $(document).ready(function()
        var userId= 1; //this can be anything like a gobal variable or an user input, etc
        $.ajax(
            url: "ajax.php",
            type: "GET",
            data: id : userId, //userId is the variable that you are going to pass to ajax.php
            dataType: "json",
            success: function(data) 
                //your logic what do you want to do with it
                $("#result").html( data.title + '-' + data.name);
            ,
            error: function(xhr, textStatus, errorThrown) 
                alert( "Request failed: " + textStatus );
            
        )
    );
</script>
</body>
</html>

ajax.php

<?php
//you logic here, this is just a dumy example

$id = isset($_GET['id']) ? $_GET['id'] : 0;
$db_records = array(
    array(
        "id" => 2,
        "title" => 'Jeremy Pinto',
        "img" => '',
        "description" => '',
        "school" => '',
        "location" => 1,
        "url" => '',
        "tablename" => 2,
        "votes" => 4,
        "name" => 'Presidential Candidates',
        "Numopinions" => 6
    ),

    array (
        "id" => 2,
        "title" => 'Sarah Bushels',
        "img" => '',
        "description" => '',
        "school" => '',
        "location" => 1,
        "url" => '',
        "tablename" => 2,
        "votes" => 3,
        "name" => 'Presidential Candidates',
        "Numopinions" => 6
    )
);

print json_encode($db_records[$id]);

【讨论】:

以上是关于如何使用 AJAX GET 请求来显示获取的 stdclass 对象的主要内容,如果未能解决你的问题,请参考以下文章

react中向后台服务器发送一请求 后台接口返回的是byte[]类型的图片 我现在如何在前台界面中显示它?

JavaScript 中使用Ajax进行网络post请求和get请求

ajax请求方式

如何在快速服务器中进行 ajax 获取/发布请求?

ajax_get/post_两级联动

如何解决微信上跨域请求withCredentials带不上cookie