Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$http request status 0 with https

THE SITUATION:

I am working on a Ionic app that receive data from an API.

Before, the API was on http:// address and everything was working fine.

Then we have moved the API to https:// and it's not working anymore. Or well, it is still working accessing it in the browser, but not in the phone (or emulator).

I am not sure what may be the problem. In the console log I see that the status of the request is 0.

It may be related with the whitelist, with the headers, or with CORS. I have tried several approaches but none worked.

WHITELIST:

Before in the config.xml there was this whitelist:

<allow-navigation href="http://*/*" /> 

I have tried to modify it in several ways but that didn't fix the problem. For example I have tried:

<allow-navigation href="https://*/*" /> 

and

<allow-navigation href="*" />

API REQUEST:

This is one example of API request:

$http.get( 'https://MY_DOMAIN.com/mobile/list_mobile_project/' ,{},{"headers" : {"Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8" }})
     .success(function(data, status, headers, config) 
     {
             // code
     }).
     error(function(data, status, headers, config) 
     {
         console.log('Error with the API list_mobile_project');
         console.log(data);
         console.log(status);
         console.log(headers);
         console.log(config);
     });

API RESPONSE:

And this is an example of API response:

public function list_mobile_project()
{
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    // code

    echo json_encode( $project_list );
}

THE QUESTION:

How to get the API working also on HTTPS?

If it is a CORS related problem, how can I enable it on server side?

like image 761
FrancescoMussi Avatar asked Feb 02 '16 14:02

FrancescoMussi


People also ask

What does it mean when an HTTP request returns status code 0?

Usually error code 0 means there is no response. (or means the connection is very slow, or the response body is empty, which mostly the reason should be specific).

What is a status code 0?

As detailed by this answer on this page, a status code of 0 means the request failed for some reason, and a javascript library interpreted the fail as a status code of 0.

How do I get the HTTP status code of a request response?

The Status-Code element in a server response, is a 3-digit integer where the first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit: S.N.

What are HTTP response status codes?

Informational responses ( 100 – 199 ) Successful responses ( 200 – 299 ) Redirection messages ( 300 – 399 ) Client error responses ( 400 – 499 )


1 Answers

When you switch from Http to Https, mostly the issue is with Hand Shake or certificate compliance. Please check if your SSL certificate is available in your trust store plus check whether your android device supports certificate provider. For example CA certificates were not supported for initial android platforms.

May be following two links can give you some idea.

http://nelenkov.blogspot.in/2011/12/using-custom-certificate-trust-store-on.html

http://www.codeproject.com/Articles/826045/Android-security-Implementation-of-Self-signed-SSL

like image 84
Rakesh Avatar answered Oct 19 '22 06:10

Rakesh