Abhishek Gupta Abhishek Gupta - 6 months ago 28
jQuery Question

$.post getting canceled in chrome

I am calling a php file using

$.post
. From the server I am returning json response. When I open the server url directly in browser it returns successfully. But from js it is not working.
enter image description here
The link I am calling is this(
http://54.249.240.120/qcorner/login
).

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<input type="button" id="testID">
<script type="text/javascript">
$(document).ready(function() {
$("#testID").click(function() {
$.post('http://54.249.240.120/qcorner/login',function(result){
alert(result);
});
});
});
</script>
</body>
</html>


I also tried in Firefox. In that I get 200 OK, but no response.

Why this is happening

Answer

The problem is that it is a cross domain request the error being returned can be viewed by turning on the javascript console:

XMLHttpRequest cannot load http://54.249.240.120/qcorner/login. Origin null is not allowed by Access-Control-Allow-Origin.

You will need to make sure that the Access-Control-Allow-Origin headers are set to allow this to happen or Chrome will cancel the request.