【问题标题】:Unexpected number showing up at the end of JSON bodyJSON 正文末尾出现意外数字
【发布时间】:2021-03-02 13:02:00
【问题描述】:

我正在尝试编写一个使用 php 和 ajax 读取 json 文件的简单演示。在js中我有

// initial entry point
function main(){
    var button = document.getElementById("button");
    button.addEventListener("click",test);
    button.addEventListener("click",getSummary);
}

function test(){
    console.log("button was clicked");
}

function getSummary(){
    asyncRequest = new XMLHttpRequest();
    asyncRequest.addEventListener("readystatechange", processResponse, false);
    console.log("sending request");
    asyncRequest.open("GET","http://localhost/summary.php",true);
    asyncRequest.send(null);
}

function processResponse(){
    console.log("processing request");
    if(asyncRequest.readyState==4 && asyncRequest.status==200){
        console.log(asyncRequest.response);
    }
}

summary.php

<?php
$data = readfile("summary.json");
header('Content-Type: text/json;charset=utf-8');
echo json_encode($data);

summary.json

{
    "products":[
        {
            "product":"professional pencil",
            "image":"pencil.jpg",
            "description":"The most verstile tool any programmer can have. With this professional pencil you'll be able to sketch out plans and fix mistakes!"
        },
        {
            "product":"coffee mug",
            "image":"coffee_mug.jpg",
            "description":"Keep your programming skills sharp and your coffee hot with this one of a kind coffee mug."
        },
        {
            "product":"programming book",
            "image":"programming_book.jpg",
            "description":"Learn how to program effectively by reading this book."
        }
    ]
}

当我提出请求时,我在响应结束时收到一个不寻常的706curl localhost/summary.php | Select Content -Expand Content | jq 产生

{
  "products": [
    {
      "product": "professional pencil",
      "image": "pencil.jpg",
      "description": "The most verstile tool any programmer can have. With this professional pencil you'll be able to sketch out plans and fix mistakes!"
    },
    {
      "product": "coffee mug",
      "image": "coffee_mug.jpg",
      "description": "Keep your programming skills sharp and your coffee hot with this one of a kind coffee mug."
    },
    {
      "product": "programming book",
      "image": "programming_book.jpg",
      "description": "Learn how to program effectively by reading this book."
    }
  ]
}
706

对此的任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过逐个输入以查看添加数字的位置?
  • 它通常是为一些调试添加的流氓回声
  • 706 是您编码为 JSON 的内容。您的变量$data 不包含您认为的内容,阅读readfile 的手册会告诉您原因。
  • 使用$data = file_get_contents("summary.json");

标签: php json ajax


【解决方案1】:

对于您的 summary.php,您实际上希望它更像以下内容来执行您打算执行的操作

<?php
$data = file_get_contents("summary.json");
header('Content-Type: text/json;charset=utf-8');
echo $data;

【讨论】:

    【解决方案2】:

    当数据已经是 JSON 文本时,不需要json_encode

    而且,更重要的是,readFile 返回一个数字,指示读取的字节数。这就是$data 中的数字,然后您将输出并在结果中看到该数字。 readFile 将实际文件数据直接发送到输出,而不是变量。

    因此您的代码可以缩短为简单

    header('Content-Type: text/json;charset=utf-8');
    readfile("summary.json");
    

    文档:https://www.php.net/manual/en/function.readfile.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      相关资源
      最近更新 更多