0

I am trying to use the $.getJSON function to fetch data from the Wikipedia API which uses HTTPS. I used this same snippet of code to request JSON data from the OpenWeather API which uses HTTP. When I check the browser console after sending the request I get this error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=doom&srnamespace=0. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

Is the Wikipedia API's use of HTTPS causing this? My code is listed below. Thank you for the help.

$("#submit").click(function() {
    var searchQuery = $("#input").val();
    var apiUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=" + searchQuery + "&srnamespace=0";

    $.getJSON(apiUrl, function(json) {
        ...
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    this has NOTHING to do with https: you're trying to do an ajax to a site/url that's different from the one you loaded this JS code from, therefore it's blocked for not having the same origin. what you want is not possible unless the url you're hitting supports JSONP (note the P) which designed to bypass this limitation. – Marc B Jun 17 '16 at 19:21
  • HTTP vs. HTTPS isn't the issue. The issue is CORS. Wikipedia doesn't support it, Openweather does. You can use JSONP, see http://stackoverflow.com/questions/37106041/does-wikipedia-api-support-cors-or-only-jsonp-available – dmeglio Jun 17 '16 at 19:22
  • @MarcB that's only half accurate. CORS also works. The problem is, Wikipedia does not support CORS. – dmeglio Jun 17 '16 at 19:23
  • You need to add a callback to get around CORS restrictions, which you can do by adding `&callback=?` to the end of your URL to make it a JSONP request. – litel Jun 17 '16 at 19:26
  • @litel that in itself is not enough. CORS is enabled/disabled on the server. Any parameter you change in the request will make no difference. Also, what you describe is JSONP, which is an entirely different format to JSON and nothing at all to do with CORS. They are not interchangeable. – Rory McCrossan Jun 17 '16 at 19:29
  • @RoryMcCrossan You're right. Got my terminology mixed up. – litel Jun 17 '16 at 19:40

0 Answers0