繁体   English   中英

如何在android的phonegap中使用休息网络服务?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM