【问题标题】:How to consume a rest web service in phonegap for android?如何在 Android 的 phonegap 中使用休息网络服务?
【发布时间】:2016-03-11 11:19:41
【问题描述】:

我是电话间隙的新手,并试图在我的电话间隙 android 应用程序中使用 application/x-www-form-urlencoded 中的 post 参数调用在 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”,来源:file:///android_asset/www/a.html (1)。

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

谢谢!!!

【问题讨论】:

  • 去掉dataType参数试试
  • 不,先生,它仍然显示错误消息 - “Uncaught SyntaxError: Unexpected token o”
  • 你能发布你得到的错误细节吗?
  • Uncaught SyntaxError: Unexpected token o this error is not related to ajax 检查你的语法是语法错误
  • [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token u",来源:file:///android_asset/www/a.html (1)

标签: javascript json ajax web-services cordova


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    终于搞定了!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2015-12-24
      • 2018-07-11
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多