JQuery ajax call in Phonegap for RESTful API

I am trying to make a RESTful api call from android emulator (using Android 2.2). On my server for the login request, I set the header cors response.setHeader("Access-Control-Allow-Origin", "*");

This exact code works fine with Firefox 4 and Chrome 10, and I’m lucky that the Android browser allows this header from version 2.1 up .

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

The server never receives the request, and the status code is 0, which I read, may indicate a cross-script error. But, as I said, it works great in modern browsers.

These are the relevant logs that I see in LogCat

 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 

As you can see, there is not much ... it hurts to try and debug something.

Edit: Permissions AndroidManifest.xml

  <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+4
source share
2 answers

This was because the Android emulator was unable to connect to the Internet. This has been fixed by adding -dns-server XXXX (where XXXX is my DNS server) to the "Default emulator settings" in the Android settings in eclipse.

+1
source

As you mentioned, you call the RESTful API to log in, I think you need to specify the ajax type for the POST method, because this is the default GET method. i.e.,

$. ajax ({type: 'POST', ...});

I think, therefore, the server does not return any response.

If this does not fix your problem, could you please tell me if you can successfully access other web services (anything on the Internet) from the application?

0
source

Source: https://habr.com/ru/post/1345787/


All Articles