Hi.
I’m having trouble with a $http post call toward a rest service. It happens only on Android systems (4.2.2 and 4.3), it works on IoS.
I’ve checked the config.xml:
This is the call:
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
console.log("crmtalker -- creo l'oggetto $http");
$http({
method: "post",
url: host,
headers:{"Content-Type":"application/x-www-form-urlencoded; charset=utf-8"},
params: chiamataObj,
responseType: "JSON"
}).success(function(data, status, headers){
console.log("crmtalker --> login ok");
console.log(data);
console.log("crmtalker <-- login ok");
}).error(function(data, status, headers, config){
console.log("crmtalker --> login ko");
console.log(JSON.stringify(data));
console.log(status);
console.log(headers);
console.log(JSON.stringify(config));
console.log("crmtalker <-- login ko");
});
The fact is, the console.log in the error side show nothing but the config object. All I get to see is a error log but it refers to an object and it s not stringified.
I also modified the rest server in order to have all the headers needed to a cors call.
I’ve run out of options, any idea about what it’s going wrong?
If you enable USB debugging on the device and use the chrome://inspect
tools from your computer, you should be able to capture the actual response and see what error the server is sending back, if any.
EDIT
Sorry, I reread your question and you’re probably already using the chrome://inspect
. In that case, go to the network tab, find the POST call, and click it. You should get a view where you can see the headers, data sent, and response.
Hi.
Thanks for replying me.
I tried to use chrome://inspect, i used ionic serve and used chrome from my android device to test the app. That way the app worked.
If I run ionic run android -l -c I get an error. The error occurs between the very last log i print before creating the $http object and the console log “login ko”.
When I print data, status, headers and config I can see that, out of the config object, they are all empty.
To me this looks like it fails to launch the net call, but I cannot find a way to print the error.
Hi. I’ve managed to see more by applying an interceptor. I get a status 0 error, it looks like a CORS problem yet I did these things:
angular.module('clb.navigation',[])
.config(function($stateProvider, $urlRouterProvider, $httpProvider){
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
The server responds with these headers if I test the app with a browser:
Access-Control-Allow-Origin *
Access-Control-Allow-Methods GET, POST, OPTIONS
Access-Control-Allow-Headers Content-Type
Content-Type application/json; charset=UTF-8
But what surprise me is the fact that if I run android as a standalone app from the device I shouldn’t have any CORS issue…
Solved.
I had to add the following headers to the server:
header(‘Access-Control-Allow-Headers: X-ACCESS_TOKEN, Access-Control-Allow-Origin, Authorization, Origin, x-requested-with, Content-Type, Content-Range, Content-Disposition, Content-Description’);
header(‘Access-Control-Allow-Methods: GET, POST, OPTIONS’);
header(‘Access-Control-Allow-Origin: *’);
Without those, $http on android will reject the response.
1 Like
Ah, when you use
ionic run android -l -c
you are using you will run into CORS issues because you are serving the app from your development computer. It’s basically the same as using ionic serve
except the proper Cordova js files and plugins are enabled. So the origin is 192.168.1.3 or whatever your computer’s IP address is. If you use
ionic run android
without -l -c
, it should work with no sever modifications.
1 Like