Widget:APITest: Difference between revisions

From Buddha-Nature
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 4: Line 4:


/**
/**
* The function to wrap the result
* The function to wrap the result
*/
*/
var callback = function (response) {
var callback = function (response) {
    var pages = response.query.random; // Process the output to get the titles
var pages = response.query.random; // Process the output to get the titles
    Object.keys(pages).forEach(function(key) {
Object.keys(pages).forEach(function(key) {
        console.log(pages[key].title);
console.log(pages[key].title);
    });
});
};
};


Line 16: Line 16:
scriptTag.src = apiEndpoint + "?" + params + "&callback=callback"; // Point to the query string
scriptTag.src = apiEndpoint + "?" + params + "&callback=callback"; // Point to the query string


document.body.appendChild(scriptTag); // Add the script tag to the document
document.getElementById("test").appendChild(scriptTag); // Add the script tag to the document
</script>
</script><div id="test"></div>
<div id="test"></div>

Revision as of 16:53, 16 December 2022

<script type="text/javascript"> var apiEndpoint = "https://en.wikipedia.org/w/api.php"; var params = "action=query&list=random&rnlimit=3&format=json";

/**

  • The function to wrap the result
  • /

var callback = function (response) { var pages = response.query.random; // Process the output to get the titles Object.keys(pages).forEach(function(key) { console.log(pages[key].title); }); };

var scriptTag = document.createElement("script"); // Dynamically create a "script" tag scriptTag.src = apiEndpoint + "?" + params + "&callback=callback"; // Point to the query string

document.getElementById("test").appendChild(scriptTag); // Add the script tag to the document

</script>