Phonegap Cross-domain AJAX POST Request not working on Android Phonegap Cross-domain AJAX POST Request not working on Android android android

Phonegap Cross-domain AJAX POST Request not working on Android


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);   }});


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>


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.