I am currently working on detecting when an XMLHttpRequest() fails due to a Cross Origin Error rather than a bad request. Take, for example:
ajaxObj=new XMLHttpRequest()
ajaxObj.open("GET", url, true);
ajaxObj.send(null);
Let's consider four scenarios regarding the URL:
Scenario 1: The URL is a valid address with the proper access-control-allow-origin set up.
- Example:
http://192.168.8.35
where the server hasAccess-Control-Allow-Origin: *
in the header - This situation can be easily identified by checking if ajaxObj.readyState==4 and ajaxObj.status==200
Scenario 2: The URL is an invalid address on an existing server.
- Example:
http://xyz.google.com
where the server responds but the request is invalid - In this case, ajaxObj.readyState==4 and ajaxObj.status==0
Scenario 3: The URL points to a non-existing server IP address.
- Example:
http://192.168.8.6
on a local network where there is no response - This results in ajaxObj.readyState==4 and ajaxObj.status==0
Scenario 4: The URL is a valid address where access-control-allow-origin is NOT set
- Example:
http://192.168.8.247
with a server that does not haveAccess-Control-Allow-Origin: *
set in the header - This leads to ajaxObj.readyState==4 and ajaxObj.status==0
The issue at hand is: How can one differentiate between Case 4 (access-control-allow-origin error) and Cases 2 & 3?
For Case 4, Chrome's debug console displays the following error:
XMLHttpRequest cannot load http://192.168.8.247/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
How can this error be flagged in Javascript?
I attempted to find some indication within the ajaxObj
object, but nothing seems to distinguish it from Cases 2 & 3.
Here is a simple test I conducted:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CORS Test</title>
<script type="text/javascript">
function PgBoot()
{
// doCORS("http://192.168.8.35"); // Case 1
// doCORS("http://xyz.google.com"); // Case 2
doCORS("http://192.168.8.6"); // Case 3
// doCORS("http://192.168.8.247"); // Case 4
}
function doCORS(url)
{
document.getElementById("statusDiv").innerHTML+="Processing url="+url+"<br>";
var ajaxObj=new XMLHttpRequest();
ajaxObj.overrideMimeType('text/xml');
ajaxObj.onreadystatechange = function()
{
var stat=document.getElementById("statusDiv");
stat.innerHTML+="readyState="+ajaxObj.readyState;
if(ajaxObj.readyState==4)
stat.innerHTML+=", status="+ajaxObj.status;
stat.innerHTML+="<br>";
}
ajaxObj.open("GET", url, true);
ajaxObj.send(null);
}
</script>
</head>
<body onload="PgBoot()">
<div id="statusDiv"></div>
</body>
</html>
Results using Chrome:
Processing url=http://192.168.8.35
readyState=1
readyState=2
readyState=3
readyState=4, status=200
Processing url=http://xyz.google.com
readyState=1
readyState=4, status=0
Processing url=http://192.168.8.6
readyState=1
readyState=4, status=0
Processing url=http://192.168.8.247
readyState=1
readyState=4, status=0