我試圖從android模擬器(使用android 2.2)進行RESTful api調用。在我的登錄請求服務器我設置cors headerresponse.setHeader("Access-Control-Allow-Origin", "*");
Jquery ajax在Phonegap內調用RESTful API
這個確切的代碼工作正常,從Firefox 4和Chrome 10,我被帶領相信Android瀏覽器解析從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);
}
});
服務器從來沒有收到請求,狀態碼是0,我讀過可能意味着一個交叉腳本錯誤。但是,正如我所說的,它在現代瀏覽器中運行良好。
這些都是相關的日誌,我在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
看到正如你可以看到有不是一大堆那裏...使它成爲一個痛苦的嘗試調試任何東西。
編輯: 的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" />
你能輸出error.message而不是錯誤嗎? – katsuya 2011-03-31 18:47:29
@knoguchi console.log(error.message);給我:undefined – Josh 2011-03-31 19:11:11
噢,我把它與本地存儲混合起來。你有沒有在Android清單xml文件中設置互聯網訪問權限? – katsuya 2011-03-31 19:55:04