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

时间:2016-03-11 11:19:42

标签: javascript json ajax web-services cordova

我是手机间隙的新手,并尝试在我的手机间隙安卓应用中使用 application / x-www-form-urlencoded 中的帖子参数调用在php中创建的休息网络服务但未获得响应。以下是调用服务的代码:

     $.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)。

如果有任何好的教程/示例,请建议我。

谢谢!

2 个答案:

答案 0 :(得分:0)

由于您只是在Phonegap中进行开发和应用,因此请在页面中的div标签中打印响应,而不是使用Console。对于如何使用jquery发送请求,您可以查看此处给出的示例: http://labs.jonsuh.com/jquery-ajax-php-json/

答案 1 :(得分:0)

最后我完成了它!

 $.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());
                }
        });