2

I can't get the following lines of code to return JSON, or even to run my simple alert() function embedded inside. I know the URL is correct, because when I paste it in the browser it returns JSON. Does anyone know what could be the problem here?

I've tried this in CodePen and JSFiddle (making sure to include jQuery) on Chrome, but no such luck.

$.getJSON("https://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&callback=?inprop=url&pageids=18630637"
, function(data) {
  alert("success");
  console.log(data);          
});
Danora
  • 315
  • 3
  • 9
  • Do you get an error message in the console? – Jonathan Lam Jul 09 '16 at 21:46
  • 1
    *XMLHttpRequest cannot load https://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&callback=?inprop=url&pageids=18630637. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.* – freedomn-m Jul 09 '16 at 21:49
  • 1
    Possible duplicate of [Wikipedia API + Cross-origin requests](http://stackoverflow.com/questions/23952045/wikipedia-api-cross-origin-requests) – freedomn-m Jul 09 '16 at 21:50
  • Not really a dupe. OP already used jsonp instead of json as suggested in dupe question, but was missing an `&` after the callback parameter in his querystring. – Jaqen H'ghar Jul 09 '16 at 22:20

3 Answers3

5

You have a missing & in your querystring. Change:

&callback=?inprop...

To:

&callback=?&inprop...

See JSFiddle

Jaqen H'ghar
  • 16,186
  • 8
  • 49
  • 52
  • What is this parameter? – Birju Shah Jul 09 '16 at 21:54
  • Can you explain how/why this works? Is it a wikipedia specific fix? – freedomn-m Jul 09 '16 at 21:56
  • @freedomn-m the parameter is for using JSONP instead of JSON. take a look in [this SO question](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) and [this SO question](http://stackoverflow.com/questions/11916780/changing-getjson-to-jsonp) Hope it helps :) – Jaqen H'ghar Jul 09 '16 at 21:59
  • Ah - so a duplicate - I just got a different duplicate. Can also change to `$.ajax(jsonp: "callback", dataType: 'jsonp', ...` which worked in jsfiddle – freedomn-m Jul 09 '16 at 22:02
  • @freedomn-m yes but OP was using `$.getJSON` :) anyway not really dupe because he already knew that he should use a callback param, only the querystring was wrong – Jaqen H'ghar Jul 09 '16 at 22:05
  • @JaqenH'ghar Thanks that was it. I didn't even think to look for that since my browser was still returning JSON. Thank you so much. Was pulling my hair out over this one. – Danora Jul 09 '16 at 23:55
  • 1
    @Top19 Our smallest problems are the hardest to find alone :) You're welcome – Jaqen H'ghar Jul 10 '16 at 08:24
1

You are receiving bad json because the url is wrong. try with this url:

https://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&callback=?&inprop=url&pageids=18630637

toto
  • 1,180
  • 2
  • 13
  • 30
0

Link returns invalid json according to http://jsonlint.com/

Error: Parse error on line 1: /**/inpropurl({ "b ^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'


/**/inpropurl({"batchcomplete":"","query":{"pages":{"18630637": {"pageid":18630637,"ns":0,"title":"Translation","contentmodel":"wikitext","pagelanguage":"en","pagelanguagehtmlcode":"en","pagelanguagedir":"ltr","touched":"2016-07-08T07:40:24Z","lastrevid":726282436,"length":83042}}}})
Daniel
  • 10,641
  • 12
  • 47
  • 85