I’m having trouble getting data from my getJSON call on my Wikipedia viewer. The link takes me to the data I want when I paste it in a ne window, if I mess with the link I get the fail alert, but otherwise nothing is showing up in the console whether I try to log the data or just a word. Can anyone help? This is what I have:
$(document).ready(function() {
$(“form”).submit(function() {
var input = $("#wikisearch ").val();
var link = “https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search="+input+"&limit=5&callback=%3F ”;
$.getJSON(link, function(data) {
$.fail(alert(“Try again”));
console.log(data);
});
AlannaL
February 1, 2017, 8:40pm
#2
You need to use jsonp, so use an ajax request instead of getJSON
I don’t know enough to explain it properly, but hopefully this link helps!
jaytsecan:
The reason it worked is that you are using JSONP, which is quite different from normal JavaScript Ajax calls (JavaScript XMLHttpRequest object). A brief explanation (somewhat technical): Even though jQuery uses the $.ajax notation, this is not really an Ajax call. It is JSONP (when the datatype property is set to JSONP). The way JSONP works is that it dynamically adds a < script > tag into the header of your HTML file. When your dynamically generated script tag (by jQuery) is execute…
1 Like
Thanks, I actually got it working using getJSON and an origin parameter. thanks though!
1 Like