1 回答

TA貢獻1868條經驗 獲得超4個贊
您好,由于$.ajax函數的async屬性默認為true,即異步請求,并且您聲明的jsonData是局部變量,所以jsonData輸出結果為undefined。如果您想外部訪問到ajax響應值,有兩種方法:
方法一:指定一個回調函數,示例如下:
$.ajax({
type: "get",
dataType: "json",
url: 'skin/data/winner.json',
success: callBack
});
// 回調函數
function callBack(jsonData) {
// 在這里做其他處理
var html = buildHtml(jsonData);
$("ul.infoList").html(html);
$(".topLoop").slide({
mainCell: ".bd ul",
effect: "topMarquee",
vis: 6,
interTime: 40,
autoPlay: true
});
}
方法二:改成同步請求,示例如下:
var jsonData; // 全局變量
$.ajax({
type: "get",
dataType: "json",
async: false,
url: 'skin/data/winner.json',
success: function (data) {
jsonData = data;
}
});
console.log(jsonData);
var html = buildHtml(jsonData);
$("ul.infoList").html(html);
$(".topLoop").slide({
mainCell: ".bd ul",
effect: "topMarquee",
vis: 6,
interTime: 40,
autoPlay: true
});
添加回答
舉報