简体   繁体   English

使用jQuery从PHP文件获取JSON数据

[英]get json data from php file with jquery

I've already looked all answers about this and it is not helping me. 我已经找到了所有有关此问题的答案,但并没有帮助我。 I'm trying to get json data from PHP file. 我正在尝试从PHP文件获取json数据。 It's working but I can't output all data. 它正在工作,但是我无法输出所有数据。

The array that I get from PHP file looks like this: 我从PHP文件获得的数组如下所示:

connected<pre>Array
(
[0] => Array
    (
        [CategoryID] => 1
        [CategoryName] => Beverages
        [Description] => Soft drinks, coffees, teas, beers, and ales
        [Picture] => beverages.gif
    )

[1] => Array
    (
        [CategoryID] => 2
        [CategoryName] => Condiments
        [Description] => Sweet and savory sauces, relishes, spreads, and seasonings
        [Picture] => condiments.gif
    )

[2] => Array
    (
        [CategoryID] => 3
        [CategoryName] => Confections
        [Description] => Desserts, candies, and sweet breads
        [Picture] => confections.gif
    )

[3] => Array
    (
        [CategoryID] => 4
        [CategoryName] => Dairy Products
        [Description] => Cheeses
        [Picture] => diary.gif
    )

[4] => Array
    (
        [CategoryID] => 5
        [CategoryName] => Grains/Cereals
        [Description] => Breads, crackers, pasta, and cereal
        [Picture] => cereals.gif
    )

[5] => Array
    (
        [CategoryID] => 6
        [CategoryName] => Meat/Poultry
        [Description] => Prepared meats
        [Picture] => meat.gif
    )

[6] => Array
    (
        [CategoryID] => 7
        [CategoryName] => Produce
        [Description] => Dried fruit and bean curd
        [Picture] => produce.gif
    )

[7] => Array
    (
        [CategoryID] => 8
        [CategoryName] => Seafood
        [Description] => Seaweed and fish
        [Picture] => seafood.gif
    )

 )
 </pre> 

and my Ajax function looks like this : 我的Ajax函数如下所示:

$.ajax({
    url:"data.php",
    type:"json",
    success:function(data){
        console.log(data)
        $.each(data, function(key, field){
            $("container").append(field.CategoryName)
        })
    }
});

I got this error: 我收到此错误:

Uncaught TypeError: Cannot use 'in' operator to search for '1678' in connectedArray Uncaught TypeError:无法使用“ in”运算符在connectedArray中搜索“ 1678”

Your data.php should look similar to this. 您的data.php应该看起来与此类似。

$arData = array(

    [0]=>array(

        'categoryID' => 1,
        'categoryName' => 'Beverages',
        'description' => 'oft drinks, coffees, teas, beers, and ales',
        'picture' => 'beverages.gif'      

    )

);

$jsonData = json_encode( $arData );
header( 'Content-Type: application/json' );
print( $jsonData );

The problem is with your PHP output, it's not valid JSON because you've got the word "connected" and HTML tags in there - strip them out, and encode everything with json_encode 问题出在您的PHP输出上,它不是有效的JSON,因为您在那里有单词“ connected”和HTML标记-去除它们,并使用json_encode对其进行编码

then add 然后加

header('Content-type: application/json');

to the top of your file. 到文件顶部。

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

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