title: Jquery ajax call within Phonegap to RESTful API
body: <p>I'm trying to make a RESTful api call from the android emulator (using android 2.2).  On my server for the login request I'm setting the <a href="http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/" rel="nofollow">cors header</a> <code>response.setHeader("Access-Control-Allow-Origin", "*");</code></p>
  
  <p>This exact code works fine from Firefox 4 and Chrome 10 and I was led to believe the android browser resolves this header from version <a href="http://caniuse.com/#search=cors" rel="nofollow">2.1 up</a>.</p>
  
  <pre><code>usr = $("#email").val();
  pwd = $("#password").val();
  
  $.ajax({
      url: "http://myremoteserver/login",
      data: {"username": escape(usr), "password": escape(pwd)},
      dataType: "json",
      headers: {"Accept": "application/json"},
      success: function(response) {
        console.log("Success: " + response);
        if (response.result == "success") {
          //doStuff
        }
        else {
          console.log("Success Error: " + response);
          $("#error").html(response);
        }
      },
      error: function(request, status, error) {
        console.log("Error status " + status);
        console.log("Error request status text: " + request.statusText);
        console.log("Error request status: " + request.status);
        console.log("Error request response text: " + request.responseText);
        console.log("Error response header: " + request.getAllResponseHeaders());
        $("#error").html(status);
      }
  
  });
  </code></pre>
  
  <p>The server never receives the request and the status code is 0 which I've read can mean a cross scripting error.  But, as I've said, it works fine in modern browsers.</p>
  
  <p>These are the pertinent logs I see in the LogCat</p>
  
  <pre><code>03-29 20:30:46.935: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 36 : Error status error
  03-29 20:30:46.935: INFO/Web Console(277): Error status error at file:///android_asset/www/index.html:36
  03-29 20:30:46.954: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 37 : Error request status text: error
  03-29 20:30:46.954: INFO/Web Console(277): Error request status text: error at file:///android_asset/www/index.html:37
  03-29 20:30:46.985: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 38 : Error request status: 0
  03-29 20:30:46.985: INFO/Web Console(277): Error request status: 0 at file:///android_asset/www/index.html:38
  03-29 20:30:47.003: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 39 : Error request response text: 
  03-29 20:30:47.003: INFO/Web Console(277): Error request response text:  at file:///android_asset/www/index.html:39
  03-29 20:30:47.034: DEBUG/PhoneGapLog(277): file:///android_asset/www/index.html: Line 40 : Error response header: 
  03-29 20:30:47.034: INFO/Web Console(277): Error response header:  at file:///android_asset/www/index.html:40
  03-29 20:33:38.704: DEBUG/SntpClient(65): request time failed: java.net.SocketException: Address family not supported by protocol
  </code></pre>
  
  <p>As you can see there isn't a whole lot there... makes it a pain to try to debug anything.</p>
  
  <p>Edit:
  AndroidManifest.xml permissions</p>
  
  <pre><code>  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
    &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /&gt;
    &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
    &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
    &lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /&gt;
    &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_CONTACTS" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>