【问题标题】:Unexpected end of JSON input at JSON.parseJSON.parse 处的 JSON 输入意外结束
【发布时间】:2018-05-01 16:00:22
【问题描述】:

我制作了我的 RESTful mysql 后端,一切都很好除了我的 feteched 查询有问题。我的意思是,当我返回的 json 超过 2 个对象时,它什么也没显示。 我正在使用 ionic3 / http。

我的 select.php:

$sql = "SELECT * FROM products ORDER BY id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
     $data = array() ;
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
    echo json_encode($data);
} else {
    echo "0";
}

我在 ionic 中的服务文件:

getProducts(){
    return this.http.get("http://localhost/api/products/select.php")
    .map(res=>{
      this.checkMe = res;

      if(this.checkMe._body !== "0"){
         return res.json()
      }

    } );
  }

我真的需要你的帮助。 谢谢大家。

【问题讨论】:

  • 我建议你一次只关注一个问题。只需在您的浏览器中打开 select.php 并在涉及 ionic 之前使其工作。如果问题出在 php 中,花时间在 js 中调试是没有意义的
  • - 首先检查特定的 php 文件是否被调用(只是通过该 url 直接到地址栏,或使用浏览器的任何调试工具来监视请求和响应状态) - 然后在其中回显一些纯文本php 文件以验证它是否可以在此处运行。 - 然后在调用查询 var_dump() 后验证数据是否良好。 - 接下来验证 json encode() 返回有效的 json。希望有帮助

标签: php mysql json angular ionic-framework


【解决方案1】:

我将 html 标记 放入 MySQL 中的一个字段中,该字段会停止 json_encode() 操作。


谢谢大家??

【讨论】:

    【解决方案2】:

    你有没有尝试过..

    getProducts(){
        return this.http.get("http://localhost/api/products/select.php")
        .map(res=>{
          this.checkMe = res.json() as any;
    
          if(this.checkMe._body !== "0"){
             return res.json()
          }
    
        } );
      }
    

    【讨论】:

    • 很高兴您回答了我的问题。我找到了解决方案。问题是:我将 html 放入了我的一个字段中,该字段停止了 json_encode() 操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2021-06-23
    • 2012-11-25
    • 2020-11-18
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多