我有mediawiki返回的以下json。
{
"query-continue": {
"allcategories": {
"accontinue": "SB_Canto_09_Verses_Appearing_in_CC"
}
},
"query": {
"allcategories": [{
"*": "S"
}, {
"*": "SAY"
}, {
"*": "SB Canto 01 Verses Appearing in CC"
}, {
"*": "SB Canto 02 Verses Appearing in CC"
}, {
"*": "SB Canto 03 Verses Appearing in CC"
}, {
"*": "SB Canto 04 Verses Appearing in CC"
}, {
"*": "SB Canto 05 Verses Appearing in CC"
}, {
"*": "SB Canto 06 Verses Appearing in CC"
}, {
"*": "SB Canto 07 Verses Appearing in CC"
}, {
"*": "SB Canto 08 Verses Appearing in CC"
}]
}
}
我想在列表中提取的数据是
S
SAY
SB Canto 01 Verses Appearing in CC
SB Canto 02 Verses Appearing in CC
SB Canto 03 Verses Appearing in CC
..
等等。
我有以下代码
$.ajax({
url: newurl,
dataType: "json",
success: function (result) {
//How do I access the elements? Since they are identified as "*"
// I tried
console.log(result.query.allcategories[0].*);
//but it does not work
//but the following apparently gives some output
console.log(result.query.allcategories[0]);
/*
returns
Object {*: "S"} using%20ajax.html:62
Object {*: "S"} using%20ajax.html:62
Object {*: "S"} using%20ajax.html:62
*/
});
});
我需要访问这些元素并将它们显示为我页面上的列表。我无法理解如何做到这一点!
也使用
的console.log(result.query.allcategories [ '*'] [0]);或
的console.log(result.query.allcategories [0] [ '*']);
无效
答案 0 :(得分:0)
你无法从ajax返回。另外,如果您的网址没有存储在本地,您将无法访问您的json文件,您可能需要查看jsonp。
你使用的Ajax不正确,我的想法不是让它返回任何内容,而是将数据交给一个叫做回调函数的东西来处理数据。
IE:
function handleData( responseData ) {
// do what you want with the data
console.log(responseData);
}
$.ajax({
url: "hi.php",
...
success: function ( data, status, XHR ) {
handleData(data);
}
});
在提交处理程序中返回任何内容都不会执行任何操作,您必须切换数据,或者直接在成功函数内执行您想要的操作。