Reputation: 271
I am looking to get the links on this WikiQuote page. I would like to see what subcategories exist under the 'Fundamental' category. They appear as links on the page, so it seemed natural to ask the API for the links. I only get back the "Category schemes" and "Main page" links, which exist in the introduction. What am I doing wrong/what have I misunderstood here?
CODE
function httpGetAsync(theUrl, callback){
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
callback(xmlHttp.responseText);
}
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send('null');
}
function callback(json_response){
stuff = json_response;
console.log(JSON.stringify(JSON.parse(json_response), null, 2));
}
httpGetAsync('http://en.wikiquote.org/w/api.php?action=query&prop=links&titles=Category:Fundamental&origin=*&format=json', callback);
Output
{
"batchcomplete": "",
"query": {
"pages": {
"4480": {
"pageid": 4480,
"ns": 14,
"title": "Category:Fundamental",
"links": [
{
"ns": 4,
"title": "Wikiquote:Category schemes"
},
{
"ns": 14,
"title": "Category:Main page"
}
]
}
}
}
}
Upvotes: 1
Views: 86
Reputation: 271
Solution
httpGetAsync('https://en.wikiquote.org/w/api.php?&action=query&list=categorymembers&cmtitle=Category:Fundamental&cmtype=subcat&origin=*&format=json', callback);
API documentation for the query used in the solution.
Explanation
This is asking for the first 10 (cmlimit not specified, so it defaults to 10 returned items) subcategories on from the Fundamental category page.
The solution addresses the issue by returning the subcategories I was after, it is not asking for the links. I am not sure why they are not appearing as links but it does get me to the end result I was after anyway.
Credits
Thanks to randelldawson on the FreeCodeCamp forums for this solution.
Upvotes: 1