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

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

本文关键字:PHP 获取 JSON 数据 jQuery 中的 get 使用      更新时间:2023-09-26

我正试图从$.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数据(请参阅代码中的注释问题)。有什么解决方案吗?

正如@joshjwalker所指出的,您可以使用

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

你的js脚本可能是

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

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

其次,这就是您从php解析json数组的方式,但您是否在php文件中使用json_encode将php数据编码为json?

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