0

I've made a fairly rudimentary url response checker that lets me know if a list of urls is responding 200, 404, 500 or one of the 30x redirects. It's working ok, but in the latter case I'm coming up against the CORS policy "Access-Control-Header" error in the console, and the script then disregards these responses because they're not one of the above. I'm not sure how to get the script to pick up on these, as my knowledge of jquery and the ajax statusCode function is fairly limited.

The main snippet of code I'm using is as follows:

$.ajax({
type: "GET",
url: urlToTest,
statusCode: {
        200: function() {
          //do something
        },
        404: function(){
           //do something
        },

etc, with options for 500 and the 8 different 30x redirects.

The error I receive in the console is:

"XMLHttpRequest cannot load . Redirect from '' to '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.

  • If there's no CORS headers in the response then you cannot call the endpoint from JS due to the Same Origin Policy. It's as simple as that. A workaround would be to proxy the request via your own server. – Rory McCrossan Jul 27 '17 at 14:10
  • @RoryMcCrossan I think the question has been misunderstood. I'm not trying to circumvent or work around the block; I simply want to be able to output a line on the page when it's encountered e.g. if I have 3 urls, 1 of which works, one of which is a 404 and one tries to redirect and hits the CORS policy block, then the script only outputs "url responded: " for the first 2 – chrisjfinlay Jul 27 '17 at 14:21
  • Yep - I'm afraid that's due to the CORS restriction. If there is no CORS headers then JS is prevented from dealing with the response in anyway. – Rory McCrossan Jul 27 '17 at 14:24
  • @RoryMcCrossan ah, ok. Is there any sort of "if none of these responses" options I can put in to just display a generic result? – chrisjfinlay Jul 27 '17 at 14:26
  • You can add an `error` property, but note that will fire for any non-2** response code – Rory McCrossan Jul 27 '17 at 14:27
  • Thanks, that'll be enough. "OK" and "NOT OK" will do for the purposes I need. – chrisjfinlay Jul 27 '17 at 14:53

0 Answers0