Skip to content

Instantly share code, notes, and snippets.

@pellekrogholt
Created February 25, 2014 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pellekrogholt/9204745 to your computer and use it in GitHub Desktop.
Save pellekrogholt/9204745 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Example of ajax CORS problem</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="example_get.js"></script>
</head>
<body>
<p>Lets say we load this page from apache: http://127.0.0.1/~user/javascript_ajax_cors/example_get.html</p>
<p>Behind the scene we make an ajax call to: http://127.0.0.1:8000</p>
<p>Which is served from some where on your box with python:</p>
<code>
python -m SimpleHTTPServer
</code>
<p>Response lookin in the log might roughly look like:</p>
<code>
XMLHttpRequest cannot load http://127.0.0.1:8000/four?foo%20bar.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access. example_get.html:1
Get was error prone likely related to CORS problems - we got errorMessage:
</code>
</body>
</html>
function ping(item){
var url = 'http://127.0.0.1:8000/'+item;
var url = 'http://murmuring-headland-9066.herokuapp.com/service/poc/DEMO_TV_BOX/'+item;
$.ajax( {
type: 'GET',
data: 'foo bar',
url:url,
processData:false,
contentType:false,
success:function(data) {
console.log('Get was succesful - we got data: '+data)
},
error:function(jqXHR, textStatus, errorMessage) {
console.log('Get was error prone likely related to CORS problems - we got errorMessage: '+errorMessage)
}
});
}
ping('four');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment