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

时间:2013-09-01 21:16:30

标签: javascript jquery ajax json get

我正在尝试从$.get() jQuery 调用中获取 JSON 数据,但我似乎无法使其正常工作。

以下是我的代码:

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);
});

这是 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);

?>    

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

此代码的一个问题是$.get()无法正常工作,因为我不断收到500内部服务器错误。一旦解决了,我不确定我是如何在包含JSON数据的PHP文件中提取JSON数据的(请参阅代码中的注释问题)。对此有何解决方案?

2 个答案:

答案 0 :(得分:1)

正如@joshjwalker所指出的那样,你可以使用

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

并且你的js脚本可能是

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

答案 1 :(得分:0)

第一步是找到您的apache错误日志。这通常会告诉你你的500服务器错误是什么,因为你的php端代码发生了某种错误。

其次,这就是你如何从php解析一个json数组,但你是否使用json_encode将你的php数据编码为你的php文件中的json?

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