NaBUru38
NaBUru38

Reputation: 99

Cordova: HTTP GET doesn't work in Android

I'm developing a mobile app on Cordova. The main code communicates with a local server through HTTP GET, for example:

    $.get( "http://192.168.2.6:8080/getStatus.sha",
        {},
        function(data) {
            alert ("checkIpAddress() success! Status: " + data.status);
        })
        .fail(function() {
            alert ("checkIpAddress() failed to get url \'" + url + "\'.");
        });

When I run that code on Mozilla Firefox, I get the server staus (and more data).

But when I run the Cordova program on an Android device, I get the "checkIpAddress() failed" message.

The local server supports CORS. The Crodova project's config.xml has these settings:

<access origin="*" />
<allow-intent href="http://*/*" />

Any suggestions to fix the error? Thanks!

Upvotes: 1

Views: 1730

Answers (1)

Simon Prickett
Simon Prickett

Reputation: 4148

If you are using Cordova 5 and the device and server can see each other (on same wifi network) you likely have to enable this with the content security policy. See Cordova Whitelist Plugin. You want to consider adding a meta tag to

Example configuration would look like:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

Depending on what else your app does or doesn't do you may not need all other options in the above, although some Ajax and templating frameworks will. The above works for a Cordova 5 app using JQuery and Handlebars.

Upvotes: 3

Related Questions