$http status 0

HI,
I’m New to Hybrid Apps development.I was facing some problem in Angular $http

Problem is like this

When i try to call the
$http.get(‘http://date.jsontest.com’)
.success(function (data, status, headers, config) {
alert(data);
})
.error(function (data, status, headers, config) {
alert(“Error occurred. Status:” + status);
});

In chrome it works

but when i check in the mobile after the build it return the status 0 can you help me please

Check CORS http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
On php resolve with: header(“Access-Control-Allow-Origin: *”);

On the device CORS should not apply, only in the browser. Am I correct?
I have the same problem, strange thing is, it worked a few days ago with ionic beta14
Was there a change?

PS: I am sorry, one should check if the wifi works correctly before assuming errors in ionic :wink:

I have been trying to trouble shoot this issue for days and it’s a problem for me.

Here’s how to reproduce.

  1. ionic start myapp sidemenu
  2. ionic platform add android
  3. Modify controller.js and call a simple endpoint which returns “{ ‘Foo’ : ‘Bar’ }”

$http.get(‘http://127.0.0.1/asimpleservice’).then(function(resp) {
console.log(‘Success’, resp);
// For JSON responses, resp.data contains the result
}, function(err) {
console.log(JSON.stringify(err));
// err.status will contain the status code
});
4. ionic run android --livereload -x

This will always return

0 148029 log {“data”:null,“status”:0,“config”:{“method”:“GET”,“transformRequest”:[null],“transformResponse”:[null],“Accept”:“application/json”,“Content-Type”:“application/json”,“url”:“http://192.168.1.19/webapi/api/mobileverification",“headers”:{“Accept”:"application/json, text/plain, /”}},“statusText”:""}

As the original poster said, this was working until I updated my ionic version. What’s going on here?

This is my solution, please test it, is functional on a RESTful server, I open permissions temporarily for test.

http://brincalatablita.com/api/products/all/format/json

function listProducts(){
var responsePromise = $http.get(“http://brincalatablita.com/mobile/all”);
responsePromise.success(function(data, status, headers, config) {
$scope.Products = data;
});
responsePromise.error(function(data, status, headers, config) {
alert("Error occurred! Status: " + status);
});
}

The RESTful server is on php, I had to add the following line to make it work;

header(“Access-Control-Allow-Origin: *”);

I think is a CORS problem.

My apologies everyone. This is absolutely a CORS issue. Once I set the allowed origin to be “*” it started to communicate.

Don’t worry, that “fix” won’t be getting deployed to production :slight_smile:

I had the same issue but a somewhat different solution.

So what happened was that if the $http request executed successfully (HTTP 200) then the ‘response’ object was populated fine.

However if the $http request was unsuccessful (e.g. HTTP 404) then the ‘response’ object would have “data = null” and “status = 0” (instead of status = 404).

So in the latter case my Ionic app would know that ‘something’ failed but wouldn’t be able to find out the reason why (i.e. no HTTP status code).

Apparently this was indeed a CORS issue (which for mysterious reasons only manifests itself with non-success responses), however I had this issue only in the browser (“ionic serve”), not ona device.

Reason was probably because on a device I use the whitelist plugin and the ‘Content-Security-Policy’ meta tag in my index.html.

Now to get it working in the browser (‘ionic serve’) I had to use the built-in proxying capability of “ionic serve” by adding an entry to the ‘proxies’ list in my “ionic.project” file, for instance:

...
  "proxies": [
    {
      "path": "/whatever-api",
      "proxyUrl": "http://api.whatever.io"
    }
  ]
...

Then when calling ‘$http’ you need to use ‘/whatever-api’ instead of ‘http://api.whatever.io’ for the URL.

For me, it was a CORS issue. In Sails.js I had to set origin: ‘*’ and headers: ‘content-type,X-Bearer-Token’

I am using third party api.
I am new in hybrid app development and using ionic framework. It works fine with google chrome (rest-api app).But it always returns status 0 with my app.

$http.get(‘http://newapi2.opencart-api.com:80/api/rest/products/category/18’,{
headers:{
‘Origin’: ‘http://newapi2.opencart-api.com’ //‘Origin’ :‘http://localhost

			}
}).then(function(resp){
console.log('Success', resp);
// For JSON responses, resp.data contains the result

}, function(err) {
console.error(‘ERR’, err.status);
// err.status will contain the status code
})

Please help me to resolve this issue.


/*The correct response header is

{
“Date”: “Thu, 12 Nov 2015 06:46:46 GMT”,
“Content-Encoding”: “gzip”,
“X-Powered-By”: “PHP/5.4.45”,
“Connection”: “Keep-Alive”,
“Content-Length”: “2694”,
“Keep-Alive”: “timeout=5”,
“Pragma”: “no-cache”,
“Server”: “Apache/2.4.16”,
“Vary”: “Accept-Encoding,User-Agent”,
“Access-Control-Allow-Methods”: “GET, POST, PUT, DELETE”,
“Content-Type”: “application/json”,
“Access-Control-Allow-Origin”: “http://localhost”,
“Cache-Control”: “no-store, no-cache, must-revalidate, post-check=0, pre-check=0”,
“Access-Control-Allow-Credentials”: “true”,
“Access-Control-Allow-Headers”: “X-Oc-Merchant-Language, X-Oc-Store-Id, Authorization, Content-Type,X-Oc-Merchant-Id, X-Oc-Session”,
“Expires”: “Thu, 19 Nov 1981 08:52:00 GMT”
}
*/

Hi there I have same problem:

Here is my_api. It work like a charm before sign app both on browser and real device. But after I signed app like described on link i get $http status 0 on real device. I apply CORS on both client and server, it is still not working. On server side I have a Glassfish server, and restful service written in Java.

Is there any advice?

Hi,
We are also facing the same kind of status code 0 issue in only production apps in release mode. We are same issue in both Android and iOS app. Apps are working fine in both develop and stage environments even in release mode. The issue is with only production environment release mode apps sometimes. Can some please help us to resolve this issue?

Most strange, in my case, is that on my iPhone device works fine, both emulators (ios and andoird) works fine, postman tests works fine. Just my android real device give this error on requests (even after I set it up with CORS header). First of all I was guessing it was an issue with the livereload tool, but now I’m not sure anymore.

Response Headers (from postman):

Access-Control-Allow-Headers → content-type, authorization
Access-Control-Allow-Origin → *
Content-Type → application/json
Date → Thu, 04 Feb 2016 13:52:57 GMT
Server → WSGIServer/0.1 Python/2.7.9
Vary → Accept
X-Frame-Options → SAMEORIGIN

Android Device Request Response:

{
“data”: null,
“status”: 0,
“config”: {
“method”: “POST”,
“transformRequest”: [
null
],
“transformResponse”: [
null
],
“headers”: {
“Content-Type”: “application/json”,
“Accept”: “application/json, text/plain, /
},
“url”: url,
“data”: data
},
“statusText”: “”
}

Sorry for api, i removed it.

As I see this problem can occur in two way.

  1. Cors issue which you can solve this way.
  2. Android refuse your request if your page is insecure. In my case I was making call directly over IP address, I update IP to a DNS name and no-more problem.

I ran into another situation where I got a status 0 in the $http response: it occurred when I didn’t have a network connection (the WIFI was switched off on my phone). In that case you just get a status code zero!

1 Like