Use CORS Anywhere.
I wanted to get a simple jQuery query that checked the latest Pingdom result for our platform. Due to CORS and the need to specify custom headers for authentication, this is not possible.
I did not want to install a proxy server for something so simple, so I found this answer and was able to use CORS Anywhere, which looks something like this:
// Will use the cors-anywhere proxy if jQuery supports it and crossDomain option is passed in. $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? 'http:' : 'https:'); options.url = http + '//cors-anywhere.herokuapp.com/' + options.url; // options.url = "http://cors.corsproxy.io/url=" + options.url; } }); // Use ajax requests as normal. $.ajax({ type: 'get', async: false, crossDomain: true, beforeSend: function(xhr){ xhr.setRequestHeader('Authorization', 'Basic encodedusername:passwordhere'); }, url: "https://api.pingdom.com/api/2.0/checks", success: function(Data) { console.log(Data); }, error: function(Data) { } });
NOTE. Do not use this if you transfer or retrieve confidential information. . If you do this, you must use your own proxy. But if you just get public data, like us, then this should be a good and clean method to get around the CORS restriction.
source share