简体   繁体   English

JavaScript无法从PHP json_encode找到JSON数据

[英]JavaScript cannot find JSON data from PHP json_encode

The idea here is to automatically load (or load at all) my index page with some products out of a MySQL database table. 这里的想法是从MySQL数据库表中自动加载(或完全加载)某些产品的索引页面。

Firstly, my PHP. 首先,我的PHP。

<?php
header('Content-Type: application/json');
require_once 'classloader.php';
$db = new Database();

$items = $db->getRows('SELECT * FROM products');

foreach($items as $eachItem){
    $itemsJSON = new Item($eachItem->product_name, $eachItem->product_quantity, $eachItem->product_cost);
    echo json_encode($itemsJSON);
}

This seems to be working great, and gives me two properly encoded JSON objects of my Item class. 这似乎工作得很好,并且为我的Item类提供了两个正确编码的JSON对象。

{"name":"Slippers","quantity":"3","cost":"4.00"}
{"name":"Gloves","quantity":"5","cost":"9.00"}

My JavaScript looks like this(and many other similar variations) 我的JavaScript看起来像这样(以及许多其他类似的变体)

$(document).ready(function () {
    $.post( "productloader.php", function( data ) {
        $( "#result" ).html( data );
    });
});

I'm not sure why it is not working. 我不确定为什么它不起作用。 I did not want to use $.getJSON() because there is no query string to work with, so I'd assume I would need $.post() . 我不想使用$.getJSON()因为没有查询字符串可以使用,所以我假设我需要$.post()

It seems like this is a pretty common issue, but I've not found a solution yet. 看来这是一个很常见的问题,但是我还没有找到解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

You can't json_encode() each item separately. 您不能单独对每个项目进行json_encode() The data you're sending to the browser is not valid JSON, just many little chunks of valid JSON one after the other. 您要发送到浏览器的数据不是有效的JSON,仅是有效JSON的许多小块。 Build an array inside your loop and then encode the whole thing. 在循环内构建一个数组,然后对整个事情进行编码。 See also http://jsonlint.com/ 另请参阅http://jsonlint.com/

<?php
header('Content-Type: application/json');
require_once 'classloader.php';
$db = new Database();

$items = $db->getRows('SELECT * FROM products');

foreach($items as $eachItem){    
    $itemsJSON[] = new Item($eachItem->product_name, $eachItem->product_quantity, $eachItem->product_cost);
}

echo json_encode($itemsJSON);

In your method of AJAX , you're outputting multiple JSON strings while the js side is expecting 1 string (data) . AJAX方法中,您要输出多个JSON字符串,而js端则需要1个字符串(data) Try removing your for loop to just: 尝试将for循环删除为:

echo json_encode($items);

You are creating JSON for each row, so you are getting separate JSON objects for each row in front end. 您将为每一行创建JSON,因此前端将为每一行获取单独的JSON对象。

You should put all row in to and array and create the JSON object outside the loop, and echo the encoded JSON string. 您应该将所有行放入and数组中,并在循环外创建JSON对象,然后回显编码后的JSON字符串。

<?php
    $allitem = $db->getRows('SELECT * FROM products');
    $itemArr=array();
    foreach($allitem as $item){    
        array_push( $itemArr ,  new Item($item->product_name, $item->product_quantity, $item->product_cost) );
    }

    echo json_encode($itemArr);
?>

or 要么

    $allitem = $db->getRows('SELECT * FROM products');
    echo json_encode($allitem );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM