i have an API which support CORS, but some of them is not support CORS yet. i do the request using ajax/jquery. how do i handle the error if the request is failed due to CORS restriction? eg, pop an alert to notify user that it's failed.
i tried this.
function poster(path, body, callback){
var url = 'http://mysite/';
url += path;
try{
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: JSON.stringify(body),
headers: {'Content-type':'application/json', 'Accept':'application/json'},
success: function(result){
callback(result);
}
});
}catch(e){
callback({'error':'Cross Origin Issue'});
}
}
this doesn't work, my browser (chrome) still throw error to the console. so, how do i handle this, so it will call the callback function instead of throwing error to the console?