简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM