How to get Pingdom validations using jQuery.Ajax?

As far as I can tell, my GET request is not allowed. But my attempts to add authorization in the headers or in the values ​​in the URL (api key, username, password) failed.

eg.

$.ajax({ type: 'get', async: false, 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) { } }); 

Can anyone advise to correct the Javascript syntax for interacting with the Pingdom API? I believe that I am trying to authorize incorrectly. Their documentation focuses on PHP, which I cannot use in this situation.

https://www.pingdom.com/services/api-documentation-rest/#authentication

+4
source share
2 answers

I don't think you can use the Pingdom API from Javascript in a web browser.

You will need to use jsonp to force your browser to allow ajax requests on different sites, but according to this answer it is not possible to set headers in a jsonp request.

0
source

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.

0
source

All Articles