Oh CORS!

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated.-Wikipedia
Because I'm trying to get or access data from another site, I have encountered this error:

“No 'Access-Control-Allow-Origin' header is present on the requested resource”

After spending bunch of hours reading what really causes it, finally found a solution. HAHA

CORS Anywhere is a NodeJS proxy which adds CORS headers to the proxied request.

//this is to allow Cross-Origin Resource Sharing
jQuery.ajaxPrefilter(function (options) {
    if (options.crossDomain && jQuery.support.cors) {
        options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
    }
});
$.ajax({
    url: <URL HERE>,
    dataType: 'json',
    type: "GET",
    success: function (e) {
    ....
    }
    ....

URL will be https://cors-anywhere.herokuapp.com/<URL HERE> in every request.
Credits to the person who created the script.

Popular posts from this blog

Publish PostGIS Layers in GeoServer Automatically using cURL and REST

Implementing Datatables in Django

Schedule Loading of Shapefile into a PostgreSQL Database