Ionic http post to external url
Solution 1
Try to add headers in your POST request.
//example of DataToSend
var DataToSend = {
userID: deviceID,
coordLat : pos.coords.latitude,
coordLon: pos.coords.longitude
};
$http({
method: 'POST',
url: 'http://url.com.br',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: DataToSend
})
Solution 2
Check this out. It is well explained how to handle issues like yours --> http://blog.ionic.io/handling-cors-issues-in-ionic/
Related videos on Youtube
Beto
Updated on May 26, 2022Comments
-
Beto almost 2 years
Im trying to send a post to a url with Ionic using angular, but i have the response:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 404.
I know that the external service is working, because i tested it by ajax and everything works perfectly...
Below the code used in AngularJS (Ionic) and Ajax:
Ionic:
var loginServiceUrl = 'http://url.com.br'; //It is not the real one var loginServiceData = { email: [email protected] senha: 1234 }; $http.post(loginServiceUrl, loginServiceData). then(function (res){ console.log(res); });
Ajax:
$.ajax({ type: "POST", url : 'http://url.com.br', //It is not the real one data : {email: '[email protected]', senha: '1234'}, success: function(result) { $('html').text(JSON.stringify(result)); } });
Does anyone know why I get the post via ajax on my localhost and not with the ionic, also localhost?
-
morgan9999 almost 7 yearsdid you find the answer?
-
Beto almost 7 yearsYep. A chrome extension: "Allow-Control-Allow-Origin: *", chrome.google.com/webstore/detail/allow-control-allow-origi/…
-
morgan9999 almost 7 yearsCool, thanks a lot!
-
-
Beto almost 8 yearsi tried that, but my low knowledge of AngularJs made me not get to do what I wanted using the factory... =/ And using just the proxy int ionic.project dosent work