Error accessing cori api

0

Personal I'm trying to access a wikipedia service with JQ, but this in returning cors error, could someone help me with a code to access this service correctly.

Url: link

error

Blocked cross-origin request: The Same Origin Policy prevents the remote resource from reading at link . (Reason: CORS 'Access-Control-Allow-Origin' header is not present.)

    
asked by anonymous 29.06.2018 / 18:34

1 answer

2

This is a protection that browsers have, preventing a client at another address from making a direct request to a site, except in cases where it authorizes external traffic. In these situations, it is worth trying to use JSONP. In an AJAX request with Jquery it looks like this:

var request = $.ajax({
  url: "https://pt.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=google",
  method: "GET",
  dataType: "jsonp"
});
 
request.done(function( msg ) {
  console.log(msg)
});
 
request.fail(function( jqXHR, textStatus ) {
  console.log(jqXHR, textStatus)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
29.06.2018 / 18:44