1

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

这是我的代码的样子:

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

4

2 回答 2

1

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

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

你的 js 脚本可能是

getJSON("getDetailsJSON.php", 
   {"ImageID" : escape(itemName)}, 
   function(data){
      console.log(JSON.stringify(data))
   }
);
于 2013-09-02T09:22:23.093 回答
0

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

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

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

于 2013-09-01T21:46:52.407 回答