Reputation: 33
i'm working on a cordova project and i'm trying to connect to a restful api but it keeps on returning a blank response. i'm assuming that it's an issue related to cors but i can't figure what i'm missing and none of the old same questions helped me when i run the app on browser it works fine and i get
Status Code: 200
Pragma: no-cache
Date: Thu, 18 May 2017 17:50:41 +0000
Host: localhost:8001
X-Powered-By: PHP/7.1.0RC6
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Content-Type: application/json;charset=utf-8
Access-Control-Allow-Origin: *
Cache-Control: no-store, no-cache, must-revalidate
Connection: close
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept,
Origin, Authorization
Expires: Thu, 19 Nov 1981 08:52:00 GMT
but when i run it on an android device i'm getting status 0 i'm using cordova 6.5.0 and android 5.1 i tried to remove whitelist plugin and reinstall it and i added these lines to my config.xml
<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="http://*/*" />
and CSP to my index.html file
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src
'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *">
i use this code to call the api:
function testAPI(){
var ajax = new XMLHttpRequest();
ajax.open("GET","http://localhost:8001/layer/hospitals",true);
ajax.onreadystatechange=function(){
if(ajax.readyState == 4) {
if (ajax.status == 200 || ajax.status == 0) {
alert(ajax.responseText);
}
}
}
ajax.send();
}
Does anyone have an idea what i'm missing ?
Upvotes: 1
Views: 1224
Reputation: 938
since localhost doesn't exist in mobile you need to replace your localhost with your ip address as following
ajax.open("GET","http://your ip address:8001/layer/hospitals",true);
Hope it help you
Upvotes: 2