[英]How to consume a rest web service in phonegap for android?
I am a newbie in phone gap and trying to call a rest web service created in php with post parameters in application/x-www-form-urlencoded in my phone gap android app but not getting the response. 我是手机间隙的新手,并试图调用在php中创建的休息网络服务,其中的应用程序/ x-www-form-urlencoded中的帖子参数在我的手机间隙Android应用程序中但没有得到响应。 Following is the code for calling the service: 以下是调用服务的代码:
$.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());
}
});
I am getting [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token u", source: file:///android_asset/www/a.html (1). 我收到[INFO:CONSOLE(1)]“Uncaught SyntaxError:Unexpected token u”,source:file:///android_asset/www/a.html(1)。
Please suggest me if there is any good tutorial/example available. 如果有任何好的教程/示例,请建议我。
Thanks!!! 谢谢!!!
Since you are developing and application in Phonegap just ,print the response in a div tag in the page instead of using Console . 由于您只是在Phonegap中进行开发和应用,因此请在页面中的div标签中打印响应,而不是使用Console。 For How to send a request using jquery you can view the example given here: http://labs.jonsuh.com/jquery-ajax-php-json/ 对于如何使用jquery发送请求,您可以查看此处给出的示例: http : //labs.jonsuh.com/jquery-ajax-php-json/
Finally I get it done! 终于我完成了!
$.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.