[英]How to consume a rest web service in phonegap for android?
我是手機間隙的新手,並試圖調用在php中創建的休息網絡服務,其中的應用程序/ x-www-form-urlencoded中的帖子參數在我的手機間隙Android應用程序中但沒有得到響應。 以下是調用服務的代碼:
$.ajax({
type: "POST",
url: "URL.php",
contentType: "application/x-www-form-urlencoded",
data: dataString,
success: function(response) {
var resp = response.responseText;
var jsonObj = JSON.parse(resp);
console.log("Success: " + jsonObj);
},
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());
}
});
我收到[INFO:CONSOLE(1)]“Uncaught SyntaxError:Unexpected token u”,source:file:///android_asset/www/a.html(1)。
如果有任何好的教程/示例,請建議我。
謝謝!!!
由於您只是在Phonegap中進行開發和應用,因此請在頁面中的div標簽中打印響應,而不是使用Console。 對於如何使用jquery發送請求,您可以查看此處給出的示例: http : //labs.jonsuh.com/jquery-ajax-php-json/
終於我完成了!
$.ajax({
type: "POST",
url: "http://www.url.php",
contentType: "application/x-www-form-urlencoded",
data: dataString,
success: function(response) {
//entered in the success block means our service call is succeeded properly
var resp = JSON.stringify(response.text); // we are accessing the text from the json object(response) and then converting it in to the string format
console.log(JSON.stringify(response)); // print the response in console
alert(resp); // alert the 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());
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.