Android设备没有执行对PHP webservice的AJAX调用

时间:2014-01-31 10:08:35

标签: javascript php ajax cordova

我是HTML 5的新手。我正在开发一个手机间隙应用程序。因为我的Web服务和数据库在服务器上,HTML页面在设备上,通过电话间隙库编译。

当我在浏览器上执行HTML页面时,我在浏览器上获得了正确的输出,但是当我通过手机间隙编译项目然后在设备上启动应用程序时没有响应。 我使用警报消息跟踪流程,因为我发现对webservice的AJAX调用在设备中不起作用。

任何人都可以告诉我在这种情况下该怎么办?

以下是我用来调用PHP webservices的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>JSONP Echo Client</title>
  <meta charset="UTF-8" />

  <script>
    "use strict";
    function jsonp(url) {
      var head = document.head;
      var script = document.createElement("script");
      script.setAttribute("src", url);
      head.appendChild(script);
      head.removeChild(script);
    }

    function jsonpCallback(data) {
      document.getElementById("response").textContent = JSON.stringify(data);
    }

//jsonp("http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello");

    jsonp("http://192.168.1.161/hotelTab/login1.php?callback=jsonpCallback&adminMasterUserName=admin&adminMasterPassword=admin");
  </script>
</head>
<body>
  <span id="response"></span>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以尝试将代码更改为更像这样并将其放入JavaScript文件中:

    $(document).on(function(){
    $(document).bind('deviceready', function(){
        //Phonegap ready
        onDeviceReady();
    });

    $( document ).bind( "mobileinit", function() {
          // Make your jQuery Mobile framework configuration changes here!

          $.mobile.allowCrossDomainPages = true;
        });

    var output = $('#output');

    $.ajax({
        type: 'GET',
        url: 'http://yourwebaddress/yourfile.php?&jsoncallback=?',
        dataType: 'JSONp',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 
                var yourvar= 'what you want'
                + 'more stuff';

                output.append(where you want it);
            });
        },
        error: function(){
           output.text('There was an error loading the data.');
        }
    });
   });
相关问题