Here is a basic jQuery $ajax function, where I purposely call a fake endpoint;
const URL_RESTEndpoint = "http://localhost/api/MyEndpointIntentionalNotExist"
$.ajax(
{
url: URL_RESTEndpoint,
method: "PATCH",
//etc...
})
.done( (data, textStatus, jqXHR) => {
//do stuff with data...
})
.fail( (jqXHR, textStatus, errorThrown) => {
//do stuff with error params...
})
.always ( function (data_xhr, textStatus) {
console.log(textStatus);
}
When the above code runs, on the Browser console (of Chrome) it prints out a long error string:
"Access to XMLHttpRequest at 'http://localhost/api/MyEndpointIntentionalNotExist'
from origin 'http://localhost:3000' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource."
Ok. Fine. But I'd like to somehow capture the above console error string within my js code, is it possible?
I've inspected all of the .$ajax objects or parameters; the best it can do is show the literal string "error" as the error. Seriously ?!
UPDATE: Thanks to the commentators and links, apparently it is a security hole to capture CORS errors in JS. No answers required, I voted to close the question, but it may help someone in future.