Skip to content

Instantly share code, notes, and snippets.

@booknara
Created February 22, 2013 01:36
Show Gist options
  • Save booknara/5010091 to your computer and use it in GitHub Desktop.
Save booknara/5010091 to your computer and use it in GitHub Desktop.
This file is for testing CORS(Cross-Origin-Resource-Sharing). You need to change Server configuration. Also, the server should have a json file like abc.json to request.
[
"bbb",
"ccc"
]
<!DOCTYPE html>
<html>
<head>
<title>COPRS test Page</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function getAvailableServices() {
// e.g., Origin URL : www.origin.com , Destination server URL : http://www.server.com/abc.json
var server_url = 'http://www.server.com/abc.json';
var method = "GET";
$.ajax({
type : method,
cache : false,
url : server_url,
async : false,
//contentType : "text/plain",
headers : {
// Custom Header
"x-zeropc-mapi-sid" : "7b7d67f8-40bc-4b6e-9398-cbf0d699f917",
},
timeout : 10000,
success : function(data, status, xhr) {
var parsedJSON = jQuery.parseJSON(data);
alert(parsedJSON);
},
error : handleError,
});
};
function handleError(data, status, xhr) {
alert(data.status);
var message;
var statusErrorMap = {
'400' : "Server understood the request but request content was invalid.",
'401' : "Unauthorised access.",
'403' : "Forbidden resouce can't be accessed",
'500' : "Internal Server Error.",
'503' : "Service Unavailable"
};
if (data.status) {
message = statusErrorMap[data.status];
if (!message) {
message = "Unknow Error \n.";
}
} else if (xhr == 'parsererror') {
message = "Error.\nParsing JSON Request failed.";
} else if (xhr == 'timeout') {
message = "Request Time out.";
} else if (xhr == 'abort') {
message = "Request was aborted by the server";
} else {
message = "Unknow Error \n.";
}
}
</script>
</head>
<body onload="getAvailableServices()">
<p>
CORS Problem Test file.
</p>
<p>
Watch embedded script element by Developer Tools like Google inspect element or fireforx fire bug
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment