vprg
vprg

Reputation: 41

phonegap build + ajax call not working on android

I'm building a phonegap app with phonegap build. My ajax call works fine on my browser and on the Ripple Emulator, but once i get the apk from phonegap build and installe it on a Samsung Galaxy 4 i get an error.

Here is my ajax call:

 var usuario = $('#usuario').val();
    var password = $('#password').val();

    $.ajax({
        url: 'http://www.example.com/page.php',
        jsonp: "callback",
        dataType: "jsonp",
        data: {nombre:usuario, password:password},
        success: function (data) {
            console.log(data);
            var respuesta = data.resp;

            if (respuesta == 0) {
                alert('Hubo un error, inténtalo de nuevo');
            };
            if (respuesta == 1) {
                var id = data.id;
                window.localStorage.setItem("usuario", id);
                window.location.replace('bici.html');
            };
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText+status+error);

        }
    });

My php headers

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST,GET,OPTIONS");
header("Access-Control-Allow-Headers: content-type");
header("Access-Control-Allow-Headers: NCZ");
header('Content-type: application/json;charset=utf-8');
date_default_timezone_set('America/Mexico_City');

and my config.xml

-->

<access origin="*"/>
<access origin="http://www.example.com" />
<!-- Added the following intents to support the removal of whitelist code from    base cordova to a plugin -->
<!-- Whitelist configuration. Refer to       https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
<plugin name="cordova-plugin-whitelist" version="1" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />

Upvotes: 1

Views: 4072

Answers (2)

vprg
vprg

Reputation: 41

I got it working!! It was along that line, i just needed to add the plig-in also.

The line for the plug-in at the config.xml was <gap:plugin name="cordova-plugin-whitelist" source="npm"/>

And the meta tag for my html <meta http-equiv="Content-Security-Policy" content="default-src data: gap: https://ssl.gstatic.com 'unsafe-eval' *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://www.example.com; connect-src 'self' http://www.example.com">

Upvotes: 2

Simon Prickett
Simon Prickett

Reputation: 4148

If you are building against Cordova 5 it's likely that you need to set at least a connect-src in the content security policy meta tag in your HTML file in your PhoneGap app, and configure it so that your app can talk to your server. Raymond Camden has a nice blog entry covering this.

By default Cordova 5 will not allow connections unless you configure the content security policy.

Here's an example configuration that goes in the HTML of your app:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://www.example.com">

Further documentation can be found here.

Upvotes: 1

Related Questions