到目前为止,我有这个,但它似乎没有用。
$.get("museums.php",function(data,status){
var response='';
//console.log(data);
var json = $.parseJSON(data);
museums = json.museums;
for(let m in museums) { $("#names-list").append("<p>" + museums[m].museum_name)
+ $("#desc-list").append("<p>" + museums[m].museum_description )
+ $("#address-list").append("<p>" + museums[m].address )
+ $("#type-list").append("<p>"+ museums[m].museum_type )
+ $("#list").append("<p>"+ museums[m].postcode )
+ $("#list").append("<p>"+ museums[m].website )
+ $("#list").append("<p>"+ museums[m].opening_hours )
+ $("#list").append("<p>"+ museums[m].closed_days )
+ $("#list").append("<p>"+ museums[m].distance_citycentre )
//+ $("#list").append('<img src="/images/' + museums[m].images + ‘/>’”)
}
选择博物馆时会运行一项功能。
function loadKelvin() {
$("#content").append('<img src="/CourseworkResources/images/' + museums[0].images + '"/>');}
它们将显示在名为&#34; content&#34;的div中。 但是返回只是没有或:[object Object] [object Object] [object Object]
PHP文件
<?php
$museumsarray = array(
array("museum_id" => "1", "museum_name" => "Kelvingrove Art Gallery and Museum", "museum_description" => "Glasgow's main art gallery and museum situated in the west end of the city.", "museum_type" => "Art Gallery",
"address" => "Argyle Street", "postcode" => "G3 8AG", "lat" => "55.8684124", "long" => "-4.29054489999998", "website" => "http://www.glasgowlife.org.uk/museums/kelvingrove/Pages/default.aspx",
"distance_citycentre" => array("driving_miles"=>"2.2", "driving_minutes"=>"12", "walking_miles"=>"1.7", "walking_minutes"=>"34"),
"opening_hours" => array("Monday"=>"10am-5pm","Tuesday"=>"10am-5pm","Wednesday"=>"10am-5pm","Thursday"=>"10am-5pm","Friday"=>"11am-5pm","Saturday"=>"10am-5pm","Sunday"=>"11am-5pm"),
"closed_days" => array("January 1st", "January 2nd", "December 24th", "December 25th", "December 26th", "December 31st"),
"images" => array(
array("image_id" => "34", "description" => "Front View Of Kelvingrove", "url" => "Kelvingrove_Front_View_1 (Medium).jpg"),
array("image_id" => "35", "description" => "Front View Of Kelvingrove", "url" => "Kelvingrove_Front_View_2 (Medium).jpg"),
array("image_id" => "36", "description" => "Front View Of Kelvingrove", "url" => "Kelvingrove_Front_View_3 (Medium).jpg"),
array("image_id" => "37", "description" => "Front View Of Kelvingrove", "url" => "Kelvingrove_Front_View_4 (Medium).jpg"),
array("image_id" => "38", "description" => "Rear View Of Kelvingrove", "url" => "Kelvingrove_Rear_View_1 (Medium).jpg"),
array("image_id" => "39", "description" => "Rear View Of Kelvingrove", "url" => "Kelvingrove_Rear_View_2 (Medium).jpg"),
array("image_id" => "40", "description" => "Organ in Kelvingrove Art Gallery", "url" => "Organ_Kelvingrove (Medium).jpg"),
array("image_id" => "41", "description" => "Heads Exhibit at Kelvingrove", "url" => "Heads_Kelvingrove (Medium).jpg"),
array("image_id" => "42", "description" => "Exhibits at Kelvingrove", "url" => "Plane_Elephant_Giraffe (Medium).jpg")
),
"keyterms" => array(1,2,4,8,12,13,16,23,26,27,29,30,31,32,33,34,36,39,40,41,43,46,49,50)
),
功能
function loadKelvin(){
$("#content").append("<p>" + "<h4>"+"Museum Name "+"</h4>" +museums[0].museum_name)
$("#content").append("<p>" + "<h4>"+"Description "+"</h4>" +museums[0].museum_description)
$("#content").append("<p>" + "<h4>"+"Address "+"</h4>" +museums[0].address)
$("#content").append("<p>" + "<h4>"+"Postcode "+"</h4>" + museums[0].postcode)
$("#content").append("<p>" + "<h4>"+"Type "+"</h4>" +museums[0].museum_type)
$("#content").append("<p>" + "<h4>"+"Website "+"</h4>"+ "<a href>" + museums[0].website+ "</a>")
$("#content").append("<p>" + "<h4>"+"Opening Hours "+"</h4>" + museums[0].opening_hours)
$("#content").append("<p>" + "<h4>"+"Closed Days "+"</h4>" + museums[0].closed_days)
$("#content").append("<p>" + "<h4>"+"Distance from City Centre "+"</h4>" + museums[0].distance_citycentre)
}
答案 0 :(得分:0)
在将结果发送到AJAX之前,您应该在PHP中使用json_encode
:
json_encode($museumsarray);
那应该可以解决你的问题。
<强>更新强>
我找到了你的问题。您没有正确解析对象。您需要使用第二个for in
循环。我测试了它并且它有效:
$.get("museums.php", function(data, status) {
var response = '';
var json = $.parseJSON(data);
museums = json.museums;
for (var m in museums) {
var imgObj = museums[m].images;
for (var x in imgObj) {
if(imgObj[x].url){
$("#list").append("<img src='/images/" + imgObj[x].url + "'/>");
}
}
}
});
我还从’
和”
这一行删除了一些奇怪的字符:
$("#list").append('<img src="/images/' + museums[m].images + ‘/>’”)
将其更改为:
$("#list").append("<img src='/images/" + imgObj[x].url + "'/>");