使用jQuery中的.get()从PHP中获取JSON数据
Getting JSON data out of PHP using .get() in jQuery
我正试图从$.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
相关文章:
- PHP:获取数据库中多行的变量
- 使用 javascript、jquery 或 php 获取设备名称和模型
- 如何使用PHP获取h1的内容以用于Twitter数据文本参数
- 从 PHP 获取当前页面 ID 后调用 jQuery 事件
- JqSuite PHP:获取列名或id
- AJAX请求php获取post/get发送的指定目录中服务器上的文件列表(无JQUERY)
- 使用AJAX从PHP获取PNG图像
- 仅使用 PHP 获取表单元格值,没有 ajax
- 使用jQuery、Javascript、PHP获取特定的部件类
- JQueryUI全尺寸日历-从PHP获取JSON数据
- 将数据从javascript发送到html并使用php获取
- PHP:获取关联数组中最后一个元素
- 如何使用java脚本和php获取多维数组数据
- AngularJS无法从PHP获取HTTP标头
- 使用PHP获取AJAX或js操作后的文档源
- 从php获取javascript中的json对象失败
- 我想使用 ajax 和 PHP 获取每个帖子数据的点赞总数
- 如何将元素的id从php获取到javascript
- 在JQuery中从PHP获取表单值
- 使用 和 javascript 和 php 获取 id 值