【问题标题】:get JSON not being imported by Jquery?让 Jquery 不导入 JSON?
【发布时间】:2012-02-12 19:09:06
【问题描述】:

我无法将 JSON 加载到我正在创建的应用中

  $("#load_basic").click(function(){

  $.ajax({
    url: 'php/show.php',
    method: 'get',
    dataType: 'json',
    success: function(json) {
      $("#textarea").html(json.doc);
    }
  });
});

我来自 show.php 的 JSON 如下

{
"id": "1",
"title": "doc 1",
"doc": "Lorem Ipsum",
"lastsaved": "2012-02-12 08:33:49"
} {
"id": "3",
"title": "doc 2",
"doc": "another lorem ipsum document",
"lastsaved": "2012-02-12 08:39:31"
}

请任何人帮忙。首先,我试图在本地进行,我认为这可能是问题所在,但现在我已经在服务器上尝试过,但仍然没有乐趣。有什么想法吗?

这是用于创建 JSON 文件的 show.php 源代码

<?php
header('Content-type: application/json');
include 'db.php';
 $query = 'SELECT * FROM docs';
 $result = mysql_query($query) or die('<p class="db_error"><b>A fatal MySQL error    occurred while trying to select <b>EVERYTHING</b> from the database.</b><br />Query: '.$query.'<br />Error: ('.mysql_errno().') '.mysql_error().'</p>');

 while ($row = mysql_fetch_assoc($result)) { 
      $arr = array('id'=>$row['id'],'title'=>$row['title'],'doc'=>$row['doc'],'lastsaved'=>$lastsaved = $row['lastsaved']);
        echo json_encode($arr);
 }//end while
?>

【问题讨论】:

  • 你得到什么输出?有什么事发生吗?
  • 不,我在控制台或任何输出中都没有收到任何错误
  • 如果这真的是你的 JSON 字符串,那么你有一个问题:它不是有效的 JSON:如果你想要一个项目列表,那么你应该有一个数组,并且项目应该由逗号。
  • 为了消除你的 JSON 无效的事实,你可以创建一个 php 数组,然后使用 json_encode 返回一个有效的 JSON 字符串
  • 继续使用 $arr[] = array(... 将行添加到 $arr,然后只调用一次 json_encode() 而不是在 while 循环内。

标签: php jquery ajax json getjson


【解决方案1】:

您上传的 JSON 不是有效的 JSON。我认为您所期望的是 JSON 数组,如下所示。

[{
"id": "1",
"title": "doc 1",
"doc": "Lorem Ipsum",
"lastsaved": "2012-02-12 08:33:49"
}, {
"id": "3",
"title": "doc 2",
"doc": "another lorem ipsum document",
"lastsaved": "2012-02-12 08:39:31"
}]

注意方括号和逗号的存在。要获得这样的输出,请在对象数组(或关联数组,取决于您使用的内容)上使用 json_encode(),而不是单独回显每个。例如,而不是:

while ($row = mysql_fetch_assoc($result)) { 
    $arr = array('id'=>$row['id'],'title'=>$row['title'],'doc'=>$row['doc'],'lastsaved'=>$lastsaved = $row['lastsaved']);
    echo json_encode($arr);
}//end while

做:

$arr = array();
while ($row = mysql_fetch_assoc($result)) { 
    $arr[] = array('id'=>$row['id'],'title'=>$row['title'],'doc'=>$row['doc'],'lastsaved'=>$lastsaved = $row['lastsaved']);
}//end while
echo json_encode($arr);

如果您有很多数据可能无法作为单个数组放入内存中,您可以尝试:

  1. 回显[字符
  2. 对于您要解析的每个对象:
    1. 回显, 字符,除非它是第一个对象
    2. 为您的对象回显 JSON
  3. 回显]字符

【讨论】:

  • 嗨,我添加了我的 php 文件的源代码(上图)我已经完成了一个数组并对其进行了 json_encode。你能告诉我我在 PHP 文件中做错了什么吗,因为我认为这就是问题所在。
  • Aaron Lumsden:我可以看到mysql_fetch_array()。正如我所说,您正在分别回显每个对象,而不是将对象数组上传到 JSON。只需阅读解决方案。
【解决方案2】:

这不是有效的 JSON。您缺少对象之间的逗号。

...
"lastsaved": "2012-02-12 08:33:49"
} {
 ^ Missing comma

您可能想发送一个对象数组,并参考json[0].doc,如:

[{ ... },  { ... }]

或者只发送一个对象,参考json.doc

{
"id": "1",
"title": "doc 1",
"doc": "Lorem Ipsum",
"lastsaved": "2012-02-12 08:33:49"
}

【讨论】:

    【解决方案3】:

    当您返回两个对象时,它们需要在一个数组中。

    您的 JSON 必须是:

    [
        {
            "id": "1",
            "title": "doc 1",
            "doc": "Lorem Ipsum",
            "lastsaved": "2012-02-12 08:33:49"
        },
        {
            "id": "3",
            "title": "doc 2",
            "doc": "another lorem ipsum document",
            "lastsaved": "2012-02-12 08:39:31"
        }
    ]
    

    【讨论】:

      【解决方案4】:

      在此处调整您的代码:

      <?php
      
      while ($row = mysql_fetch_assoc($result)) 
      { 
          $arr = array('id'=>$row['id'],'title'=>$row['title'],'doc'=>$row['doc'],'lastsaved'=>$lastsaved = $row['lastsaved']);
              echo json_encode($arr);
      }//end while
      ?>
      

      应该变成

      <?php
      
      $json_output = array();
      while ($row = mysql_fetch_assoc($result)) 
      { 
          $json_output[] = array('id'=>$row['id'],'title'=>$row['title'],'doc'=>$row['doc'],'lastsaved'=>$lastsaved = $row['lastsaved']);
      
      }//end while
      
      echo json_encode($json_output);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2019-06-26
        • 2019-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-25
        相关资源
        最近更新 更多