My web application is trying to access the information from another server belonging to a different domain. Since it is a cross domain ajax call, i have used "dataType" as "JSONP" instead of JSON.
Now in the WebInspector-> Network Tab, I see the request is successful and the response is populated. But the success() function is never called. It always calls the error function with status = 200.
Since it is a JSONP request, jQuery appends "callback=?" to the end of the URL and the callback method was never called, even after getting the valid response. Not sure what went wrong?
$(document).ready(function() {
// When you click the Get Instances button:
$('#getInstances').click(function(){
$.ajax({
type: "GET",
url: "http://178.28.167.221:8080/us-east-1/instance/list.json",
contentType: "application/json; charset=utf-8",
accept: "application/json",
crossDomain: "true",
headers: {'Access-Control-Allow-Origin','*'},
dataType: "jsonp",
success: function(msg) {
result = JSON.stringify(msg);
var obj = $.parseJSON(result);
console.log(obj);
$('#instancesTable').append('<tr><td>' + obj.autoScalingGroupName + '</td><td>' + obj.amiId + '</td><td>' + obj.instanceId + '</td><td>' + obj.instanceType +'</td></tr>');
},
error: function(xhr, ajaxOptions, thrownError, textStatus, responseText) {
console.log(name + ' environment failed with error: ' + xhr.status + " " + thrownError);
var errorMessage = "Error";
if (xhr.status == 0) {
errorMessage = "Can't Connect";
}
}
});
});
});
I also verified by including the below in my ajax request:
jsonp: 'callback',
jsonpCallback: 'jsonpCallback'
and defining the callback function as below:
function jsonpCallback(data){
alert("jsonpCallback");
}
jsonCallback was never called, even if the request is successful and response is available from the server (shown in Network Tab of WebInspector).
The response from the server is an Array of JSON objects. [{JSON1}, {JSON2} ..]
Note : I tried to change the contentType as "application/javascript" since the response was an array of JSON objects. But nothing worked :(