I tried the following code in Postman and it was working. Is there something wrong with the code?
$.ajax({ url: 'http://api.example.com/users/get', type: 'POST', headers: { 'name-api-key':'ewf45r4435trge', }, data: { 'uid':36, }, success: function(data) { console.log(data); } });
I got this error in my console as below, please advise.
XMLHttpRequest cannot load http://api.example.com/users/get Response for preflight is invalid (redirect)
I received the same error when I tried to call https web service as http webservice.
e.g when I call url 'http://api.example.com/users/get' which should be 'https://api.example.com/users/get'
This error is produced because of redirection status 302 when you try to call http instead of https.
This answer goes over the exact same thing (although for angular) -- it is a CORS issue.
One quick fix is to modify each POST request by specifying one of the 'Content-Type' header values which will not trigger a "preflight". These types are:
ANYTHING ELSE triggers a preflight.
For example:
$.ajax({ url: 'http://api.example.com/users/get', type: 'POST', headers: { 'name-api-key':'ewf45r4435trge', 'Content-Type':'application/x-www-form-urlencoded' }, data: { 'uid':36, }, success: function(data) { console.log(data); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With