dqr91899 2016-12-06 13:59
浏览 43
已采纳

我正在尝试检索图像,这些图像由他们的URL存储在php文件中。它们存储在名为images的文件夹中

So far i have this but it doesn't seem to work.

    $.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 + ‘/>’”)


 }

A function is run when a museum is selected.

function loadKelvin() {
    $("#content").append('<img src="/CourseworkResources/images/' +      museums[0].images + '"/>');}

They are to be displayed in a div called "content". However the return is simply nothing or: [object Object][object Object][object Object]

PHP file

<?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

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)



 }
  • 写回答

1条回答 默认 最新

  • dro62273 2016-12-06 14:05
    关注

    You should use json_encode in PHP before sending the result to AJAX:

    json_encode($museumsarray);

    And that should solve your problem.

    Update:

    I've found your problem. You are not parsing the object correctly. You need to use a second for in loop. I've tested it and it works:

     $.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 + "'/>");
              }
         }
       }
     });
    

    I've also removed some weird characters from this line like and :

    $("#list").append('<img src="/images/' + museums[m].images + ‘/>’”)

    Changed it to:

    $("#list").append("<img src='/images/" + imgObj[x].url + "'/>");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端