提问者:小点点

如何在phonegap for android中使用rest web服务?


我是phone gap的新手,尝试调用在php中创建的rest web服务,并在我的phone gap android应用程序中使用application/x-www-form-urlencoded中的post参数,但没有得到响应。以下是调用该服务的代码:

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

我得到[信息:控制台(1)]“未捕获的语法错误:意外的令牌u”,来源:file:///android_asset/www/a.html (1).

如果有好的教程/例子,请告诉我。

谢谢


共2个答案

匿名用户

因为您只是在Phonegap中开发和应用程序,所以在页面中的div标记中打印响应,而不是使用控制台。有关如何使用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());
                }
        });