admin管理员组文章数量:1022997
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 cors header response.setHeader("Access-Control-Allow-Origin", "*");
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 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've read can mean a cross scripting error. But, as I've said, it works fine in modern browsers.
These are the pertinent logs I see in the 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.SocketException: Address family not supported by protocol
As you can see there isn't a whole lot there... makes it a pain to try to debug anything.
Edit: AndroidManifest.xml permissions
<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" />
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 cors header response.setHeader("Access-Control-Allow-Origin", "*");
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 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've read can mean a cross scripting error. But, as I've said, it works fine in modern browsers.
These are the pertinent logs I see in the 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.SocketException: Address family not supported by protocol
As you can see there isn't a whole lot there... makes it a pain to try to debug anything.
Edit: AndroidManifest.xml permissions
<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" />
Share
Improve this question
edited Mar 31, 2011 at 19:59
Josh
asked Mar 29, 2011 at 21:17
JoshJosh
2,9528 gold badges45 silver badges52 bronze badges
8
- Can you please output error.message instead of error? – katsuya Commented Mar 31, 2011 at 18:47
- @knoguchi console.log(error.message); gives me: undefined – Josh Commented Mar 31, 2011 at 19:11
- oh yeh I mixed it up with local storage. Have you set internet access permission in android manifest xml file? – katsuya Commented Mar 31, 2011 at 19:55
- @knoguchi yea it does - I've edited my post to include the permissions section of my AndroidManifest.xml – Josh Commented Mar 31, 2011 at 20:00
- If the server hasn't got the request at all, the status code 0 possibly indicating that there is some problem with firewall. Have you try turning it off? – katsuya Commented Mar 31, 2011 at 20:07
2 Answers
Reset to default 1This was caused by the android emulator not being able to connect to the internet. It was fixed by adding -dns-server X.X.X.X
(where X.X.X.X is my dns server) to the "Default emulator options" in the Android preferences in eclipse.
As you mentioned you are calling RESTful API for login service, I think you need to specify the ajax type to be POST method because it is GET method in default. i.e.,
$.ajax({ type: 'POST', ... });
I think that is why server is not returning any response.
If this does not fix your problem, could you tell me if you can successfully access other web services (anything on the web) from the application?
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 cors header response.setHeader("Access-Control-Allow-Origin", "*");
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 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've read can mean a cross scripting error. But, as I've said, it works fine in modern browsers.
These are the pertinent logs I see in the 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.SocketException: Address family not supported by protocol
As you can see there isn't a whole lot there... makes it a pain to try to debug anything.
Edit: AndroidManifest.xml permissions
<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" />
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 cors header response.setHeader("Access-Control-Allow-Origin", "*");
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 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've read can mean a cross scripting error. But, as I've said, it works fine in modern browsers.
These are the pertinent logs I see in the 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.SocketException: Address family not supported by protocol
As you can see there isn't a whole lot there... makes it a pain to try to debug anything.
Edit: AndroidManifest.xml permissions
<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" />
Share
Improve this question
edited Mar 31, 2011 at 19:59
Josh
asked Mar 29, 2011 at 21:17
JoshJosh
2,9528 gold badges45 silver badges52 bronze badges
8
- Can you please output error.message instead of error? – katsuya Commented Mar 31, 2011 at 18:47
- @knoguchi console.log(error.message); gives me: undefined – Josh Commented Mar 31, 2011 at 19:11
- oh yeh I mixed it up with local storage. Have you set internet access permission in android manifest xml file? – katsuya Commented Mar 31, 2011 at 19:55
- @knoguchi yea it does - I've edited my post to include the permissions section of my AndroidManifest.xml – Josh Commented Mar 31, 2011 at 20:00
- If the server hasn't got the request at all, the status code 0 possibly indicating that there is some problem with firewall. Have you try turning it off? – katsuya Commented Mar 31, 2011 at 20:07
2 Answers
Reset to default 1This was caused by the android emulator not being able to connect to the internet. It was fixed by adding -dns-server X.X.X.X
(where X.X.X.X is my dns server) to the "Default emulator options" in the Android preferences in eclipse.
As you mentioned you are calling RESTful API for login service, I think you need to specify the ajax type to be POST method because it is GET method in default. i.e.,
$.ajax({ type: 'POST', ... });
I think that is why server is not returning any response.
If this does not fix your problem, could you tell me if you can successfully access other web services (anything on the web) from the application?
本文标签: javascriptJquery ajax call within Phonegap to RESTful APIStack Overflow
版权声明:本文标题:javascript - Jquery ajax call within Phonegap to RESTful API - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745545481a2155373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论