I have this simple API call that is working in POSTMAN:
Real simple. Works perfectly.
However, I've tried to replicate this in jQuery like follows:
$.ajax({
url: "https://example.us-east-1.amazonaws.com/prod",
type: "POST",
contentType: "application/json",
data: {
id_api: "catalogo_get_categorias"
},
success: function(results){
console.log(results)
},
error: function(err) {
console.log(err)
}
});
But it keeps coming back with 500 error.
Any ideas?
Edit:
The console produces the following error when I have the Chrome CORS extension disabled
And with the CORS plugin enabled this is the output
Postman isn't bound to the same-origin policy like your browser (Chrome) is. If the API endpoint that you're requesting - https://example.us-east-1.amazonaws.com/prod
- doesn't respond with a CORS header like Access-Control-Allow-Origin: *
, you'll hit the exception that you've posted.
To solve the problem, your options are basically:
Also be sure that the $.ajax()
request sets the appropriate headers by using pertinent config object properties. For example, you may need to set the xhrFields.withCredentials
property on the config object you pass to $.ajax()
:
$.ajax({
url: "https://example.us-east-1.amazonaws.com/prod",
type: "POST",
contentType: "application/json",
xhrFields: {
withCredentials: true
},
data: {
id_api: "catalogo_get_categorias"
},
success: function (results) {
console.log(results);
},
error: function (err) {
console.log(err);
}
});