简体   繁体   English

使用jQuery中的.get()从PHP中获取JSON数据

[英]Getting JSON data out of PHP using .get() in jQuery

I'm trying to get JSON data out of a $.get() jQuery call, but I can't seem to make it work properly. 我正在尝试从$.get() jQuery调用中获取JSON数据,但似乎无法使其正常运行。

Here's what my code looks like: 这是我的代码:

var url = "getDetailsJSON.php?ImageID=" + escape(itemName);

$.get(url, function (data) {

    console.log("Success!");
    var $detailDiv = $("#description");
    var itemDetails = $.parseJSON(data); // Is this how I would get the JSON object in the php code? 
    console.log("success" + data);
    var children = $detailDiv.children();
    for (var i = children.length; i > 0; i--) {
        $detailDiv.remove(children[i - 1]);
    }

    var descriptionP = $("<p></p>");
    descriptionP.text("Description: " + itemDetails.description);
    $detailDiv.append(descriptionP);

    var priceP = $("<p></p>");
    priceP.text("Price: $" + itemDetails.price);
    $detailDiv.append(priceP);
    var list = $("<ul></ul>");
    $.each(itemDetails.urls, function (index, value) {
        var url = itemDetails.urls[index];
        var li = $("<li></li>");
        var a = $("<a></a>");
        a.attr("href", url);
        a.text(url);
        li.append(a);
        list.append(li);
    });
    $detailDiv.append(list);
});

Here's the PHP code: 这是PHP代码:

<?php

require_once('JSON.php');
$json = new Services_JSON();

$itemGuitar = array(
  'id' => 'itemGuitar',
  'description' => 'Pete Townshend once played this guitar while his own axe was in the shop having bits of drumkit removed from it.',
  'price' => 5695.99,
  'urls' => array('http://www.thewho.com/',
                  'http://en.wikipedia.org/wiki/Pete_Townshend')
);

$itemShades = array(
  'id' => 'itemShades',
  'description' => 'Yoko Ono\'s sunglasses. While perhaps not valued much by Beatles fans, this pair is rumored to have been licked by John Lennon.',
  'price' => 258.99,
  'urls' => array('http://www.beatles.com/',
                  'http://johnlennon.com/',
                  'http://www.yoko-ono.com/')
);

$itemCowbell = array(
  'id' => 'itemCowbell',
  'description' => 'Remember the famous "more cowbell" skit from Saturday Night Live? Well, this is the actual cowbell.',
  'price' => 299.99,
  'urls' => array('http://www.nbc.com/Saturday_Night_Live/',
                  'http://en.wikipedia.org/wiki/More_cowbell')
);

$itemHat = array(
  'id' => 'itemHat',
  'description' => 'Michael Jackson\'s hat as worn in the "Bille Jean" video. Not really rock memorabilia, but it smells better than Slash\'s tophat.',
  'price' => 1699.99,
  'urls' => array('http://www.michaeljackson.com/',
                  'http://music.yahoo.com/vid-2143030--Billie-Jean')
);


$details = array (
  'itemGuitar'  => $itemGuitar,
  'itemShades'  => $itemShades,
  'itemCowbell' => $itemCowbell,
  'itemHat'     => $itemHat
);

$itemDetail = $details[$_REQUEST['ImageID']];
$output = $json->encode($itemDetail);
print($output);

?>    

The 500 internal server error shows: 500内部服务器错误显示:

Connection  close
Content-Encoding    gzip
Content-Length  20
Content-Type    text/html
Date    Sun, 01 Sep 2013 22:47:32 GMT
Server  Apache/2.2.22 (Ubuntu)
Vary    Accept-Encoding
X-Powered-By    PHP/5.3.10-1ubuntu3.7

One of the problems with this code is that $.get() doesn't work as expected, because I keep getting a 500 internal server error. 此代码的问题之一是$.get()不能按预期方式工作,因为我不断收到500个内部服务器错误。 Once that is solved, I'm not sure how I'm suppose to extract that JSON data in a PHP file that contains JSON data (see the comment question in code). 一旦解决,我就不确定如何将JSON数据提取到包含JSON数据的PHP文件中(请参阅代码中的注释问题)。 Any solutions for this? 有什么解决办法吗?

as pointed out by @joshjwalker, you might use 正如@joshjwalker指出的那样,您可以使用

$itemDetail = $details[$_GET['ImageID']];
echo json_encode($itemDetail);

and your js script might be 和你的js脚本可能是

getJSON("getDetailsJSON.php", 
   {"ImageID" : escape(itemName)}, 
   function(data){
      console.log(JSON.stringify(data))
   }
);

The first step is to find your apache error log. 第一步是找到您的apache错误日志。 That will usually tell you what your 500 server error is, because there is some sort of error happening on your php side code. 这通常会告诉您500服务器错误是什么,因为php端代码中发生了某种错误。

Secondly, that is how you parse a json array from php, but are you using json_encode to encode your php data as json in your php file? 其次,这就是您从php解析json数组的方式,但是您是否正在使用json_encode将php数据编码为php文件中的json?

http://php.net/manual/en/function.json-encode.php http://php.net/manual/en/function.json-encode.php

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

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