Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Cross-domain AJAX POST Request not working on Android

Cross-domain AJAX POST request works perfectly fine on web browsers including browsers on mobile phones, but doesn't work for native applications built using Phonegap

I have created a login form that users have to enter their login credentials, then they are verified by the server that is hosted on heroku and returns json {"success":true} if valid credentials are entered.

My Ajax script:

$.ajax({
   type: "POST",
   url: "http://domain.com/public/auth/app-login",
   contentType: "application/x-www-form-urlencoded; charset=utf-8",
   dataType: "json",
   data: {identity: <username from form>, password: <password from form>},
   crossDomain: true,
   cache: false,
   success: function(data) {
       obj = JSON.parse(data);
       if (obj && obj.success === true) {
          window.location.href = 'home.html';
       }
   },
   error: function(e) {
       alert('Error: ' + e.message);
   }
});

Steps taken to resolve this issue:

  • Domain whitelisting - config.xml

<access origin="http://domain.com/public/auth/app-login" />

<access origin="*" />

  • Telling jQuery to allow cross-domain

$.support.cors = true; OR jQuery.support.cors = true;

  • Disable caching cache: false

Any help is appreciated.

like image 979
h4kl0rd Avatar asked Sep 25 '13 10:09

h4kl0rd


3 Answers

Ok. If index.html in local then you can call ajax any hosts, not need enable CORS in client or server. You remove:

$.support.cors = true; OR jQuery.support.cors = true;

And:

<access origin="http://domain.com/public/auth/app-login" />

It redundant, only use:

<access origin="*" />

You need check and add in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Add more permission if your app required. Finally, call ajax inside $(document).ready():

$.ajax({
   type: "POST",
   url: "http://domain.com/public/auth/app-login",
   dataType: "json",
   data: {identity: <username from form>, password: <password from form>},
   success: function(data) {
     obj = JSON.parse(data);
     if (obj && obj.success === true) {
        window.location.href = 'home.html';
     }
   },
   error: function(e) {
     alert('Error: ' + e.message);
   }
});
like image 65
Hanh Le Avatar answered Nov 13 '22 03:11

Hanh Le


If you are looking to resolve this issue then you may wish to make sure that the plugin is being properly included into the build process.

RESOURCE: \app\config.xml
<widget>
    .... [lots of stuff] ....
    <gap:plugin name="com.indigoway.cordova.whitelist.whitelistplugin" />
    <access origin="http://*" />
    ....
</widget>

You may also wish to specify a version as that is recommended, and I do not specify one above. A good way to test if the plugin is included is to use the free cloud account provided at, https://build.phonegap.com/apps. If you build your project there you can check the plugins tab and make sure that the whitelist plugin is included.

I have read that you should only need this in the HEAD element of your HTML page, but I found as of the date of this post that I still needed to include the plugin.

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

If the plugin is not loaded you might get a "Not Found" error when using the $.ajax method for jQuery for the error string.

Some of the information on the Internet will tell you that the whitelist information is placed into the /www/res/ folder, but this appears to be outdated information. You may also find that <plugin... is used in some examples, but it appears that this may be an obsolete way?

Also, you may need:

RESOURCE: \app\config.xml
<widget>
     ...
     <feature name="http://api.phonegap.com/1.0/network"/>
     ...
</widget>
like image 42
kmcguire Avatar answered Nov 13 '22 02:11

kmcguire


use

JSON.stringify(data: {identity: <username from form>, password: <password from form>}) 

instead of data: {identity: <username from form>, password: <password from form>}

I got success message when i changed my code like this.

like image 1
Sonia John Kavery Avatar answered Nov 13 '22 03:11

Sonia John Kavery