【问题标题】:how can you use an AJAX GET request to display fetched stdclass Objects如何使用 AJAX GET 请求来显示获取的 stdclass 对象
【发布时间】: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 编码。

标签: php mysql ajax pdo


【解决方案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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      相关资源
      最近更新 更多