Phonegap中的Ajax无法正常工作

I know this question has been address many times on Stack Overflow, but none of the solutions are working for me.

I can't get ANY ajax requests to complete within my phonegap application when running on the device (Android 4.4.2), yet everything works fine from desktop browsers.

First I tried AngularJS $http

Then I tried jQuery .get

Then I tried raw xhr

In all cases, the request immediately fails with no response. I've tried requesting data from my own servers, and from google servers, and elsewhere, all the same. I've tried whitelisting my domains in config.xml, in many forms, still no effect. I've even opened the console, and manually created an XHR and tried to GET on it, and the same thing happens. The request immediately fails. If anyone can please help me out, that would be great. I'm on the latest version of pretty much all my software, having set up my dev environment just today.

Cross domain request headaches. The browser allows the cross domain request but phonegap on the device kills it. I've been there and it took some work getting used to dealing with it. Try using jquery and jsonp in an ajax get request.

Try adding <access origin="*" /> to your config.xml for testing.

Here's an example of an jquery ajax request taken from working code.
$.ajax({ timeout: 5000, url: 'https://yourdomain.com/yourfile.php', dataType: "jsonp", jsonpCallback: 'yourcallbackfunction', type: "GET", crossDomain: true, data: {'somedata':'your data string'}, error: function(xhrObj,text,errorObj){ if(xhrObj.status !== 200) { var errorTxt='Error Log: '; errorTxt=errorTxt+"Error code specific:'"+xhrObj.status+"' "; errorTxt=errorTxt+"Error status specific:'"+xhrObj.statusText+"' "; errorTxt=errorTxt+"Error code general:'"+text+"' "; errorTxt=errorTxt+"Error status general:'"+errorObj.message+"' "; console.log(errorTxt); } } }); function yourcallbackfunction(data) { // do cool stuff here console.log(data); } Be sure to handle the headers on your servers response so that the response gets back to your client. Here's a sample processing function I used to return data. Sorry if php isn't your server side scripting language but hopefully you'll get the idea. `function SendResults($data) { // Allow from any origin if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } // Access-Control headers are received during OPTIONS requests if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

}
header("content-type: application/json");
$callback = filter_input(INPUT_GET, 'callback', FILTER_SANITIZE_SPECIAL_CHARS);
echo  "$callback(" . json_encode($data) . ")";

}`

Good luck.. Hope this helps you.