这是json_encode()文件中出现的json字符串:
[{"ID":"650","DESCR":"FRONTAGE","MAINURL":"images\/98\/30-HEATH (1).JPG","THUMBURL":"images\/98\/30-HEATH (1).JPG","ALBUM":"98"},{"ID":"651","DESCR":"PICTURE","MAINURL":"images\/98\/30-HEATH (2).JPG","THUMBURL":"images\/98\/30-HEATH (2).JPG","ALBUM":"98"},{"ID":"652","DESCR":"PICTURE","MAINURL":"images\/98\/30-HEATH (3).JPG","THUMBURL":"images\/98\/30-HEATH (3).JPG","ALBUM":"98"}]
这是我正在使用的电话。它确实提供了一个Object,但每次我尝试访问它时,我都会从chrome javascript控制台中收到类型错误。
function FindPictures(b) {
var picturesHome = {};
var album = "";
var baseURL = b;
this.pics = function(alb){
album = "";
$.ajax({
async: false,
url: "picsJSON.php",
dataType: 'json',
success: function (json) {
picturesHome = json;
},
error: function (error) {}
});
return picturesHome;
};
};
答案 0 :(得分:0)
success
是一个回调:在ajax下载完成之前它不会被执行,但是你的函数会立即返回picturesHome
。标准程序是在success
回调中做一些有意义的工作:
function doSomethingWithThePictures(pics)
{
alert('got some pics!');
console.log(pics);
}
... snip ...
$.ajax({
async: false,
url: "picsJSON.php",
dataType: 'json',
success: function (json) {
doSomethingWithThePictures(json);
},
error: function (error) { alert('ERROR: ' + error); }
});
答案 1 :(得分:0)
我猜,问题在于ajax调用中指定的数据类型
这可以通过在php中指定JSON内容类型来解决,如
header('Content-Type: application/json');
或从ajax中删除datatype
选项并将其解析为JSON,如
$.ajax({
async: false,
url: "picsJSON.php",
success: function (json) {
picturesHome = JSON.parse(json); // you can use $.parseJSON() instead
},
error: function (error) {}
});
希望这会有所帮助