无法使用jquery函数getJSON()或post从子目录级别读取JSON文件

时间:2018-07-24 07:06:41

标签: javascript php jquery json

在下拉框的更改事件发生时,无法使用jquery函数getJSON()或$ .post()从子目录级别读取JSON文件

我给出了动态获取的文件路径

DIRECTORY PATH = /home/spipl8/www/football/wallpaper/spg.wallpaper.artem.dzyuba.football/more.json

script.js

$(document).on("change","#dir_list",function(){
        var dir_val = $(this).val();
        console.log(dir_val+"/more.json");
        //return false;
        $.getJSON(dir_val+"/more.json",function(data){
            var list='';
            console.log(data);
            /*for(var i=0;i<data.length;i++){
                list +='<li class="img_contain"><div class="box"><img src="'+data[i]['img']+'" key="'+i+'" ply_name="'+data[i]['name']+'" path="'+data[i]['path']+'"></div><a href="javascript:void(0)" class="remove" key="'+i+'"><img src="assets/images/delete.png"></a><label class="player_name">'+data[i]['name']+'</label></li>';
            }
            $("#sortable").html(list);*/
        });
    }); 

    more.json
    [
{"name":"Artem Dzyuba","img":"\/home\/spipl8\/www\/football\/wallpaper\/spg.wallpaper.artem.dzyuba.football\/thumb\/artem_dzyuba_0707_0015.jpg","path":"spg.wallpaper.artem.dzyuba.football"},
{"name":"Emil Forsberg","img":"\/","path":"spg.wallpaper.emil.forsberg.football"},{"name":"Gilanluigi Buffon","img":"\/home\/spipl8\/www\/football\/wallpaper\/spg.wallpaper.gilanluigi.buffon.football\/thumb\/buffon_0630_0047.jpg","path":"spg.wallpaper.gilanluigi.buffon.football"}
]

1 个答案:

答案 0 :(得分:0)

假设您正在本地运行此JS文件,则应考虑在何处运行它。您传递给$ .getJSON的 是相对路径,不是绝对路径。

因此,如果您的JS文件位于目录“ / abc”中,则getJSON将检查“ /abc/home/spipl8/www/football/wallpaper/spg.wallpaper.artem.dzyuba.football/more.json “