如何使用PhoneGap(Android应用程序)实时连接到Web服务器上的PHP文件?

问题描述:

下面给出的代码可以在本地主机上的wamp服务器上正常运行,它正在调用连接到MySql DB并返回数据的php文件.

The below given code works fine using a wamp server on localhost.It is calling a php file that connects to the MySql DB and returns data.

但是,我正在尝试使用PhoneGap构建mobileapp.下面的代码在HTML文件中.我的问题是,一旦我将其上传到phonegap并生成.apk文件,我的html文件将如何使用ajax与服务器建立连接.由于以下代码仅调用getbustime.php文件,而没有任何Web服务器参数.

However i am trying to build a mobileapp using PhoneGap. The below code is in a HTML file. My questions is how will my html file make a connection with the server using ajax once I upload it to phonegap and generate the .apk file. Since the below code just calls the getbustime.php file without any web server parameters.

我已经使用HTML,jQueryMobile,Ajax将我的应用开发到了phonegap中,现在愿意上传帮助了吗?

i have devloped my app in phonegap using HTML,jQueryMobile,Ajax and now willing to upload help ?

  function getBusTime(){



                         if (window.XMLHttpRequest)
                        {// code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp=new XMLHttpRequest();
                        }
                      else
                        {// code for IE6, IE5
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                      xmlhttp.onreadystatechange=function()
                        {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                          {

                         document.getElementById("resultLogBus").innerHTML=xmlhttp.responseText;

                    // $('#resultLogBus').html(xmlhttp.responseText).selectmenu( "refresh");
                         }
                        }

                      xmlhttp.open("GET","getbustime.php?direction="+direction+"&busnum="+busnum+"&dayofweek="+dayofweek+"&stopname="+stopname+"&starttime="+starttime+"&endtime="+endtime,true);
                      xmlhttp.send();



    }//End of function getBusTime


</script>

基本上,您需要像这样在ajax调用中设置服务器/php文件的完整路径:

Basically, you need to set a full path to the server/php file in your ajax call like so:

xmlhttp.open("GET","http://<YOUR_SERVER_NAME>/getbustime.php?direction="+direction+"&busnum="+busnum+"&dayofweek="+dayofweek+"&stopname="+stopname+"&starttime="+starttime+"&endtime="+endtime,true);


您可能还需要根据自己的情况做几件事:


A couple other things you may ALSO need to do depending on your circumstances are:

  1. 在phonegap config.xml中设置此标签:

    <access origin="*" />

  1. set this tag in the phonegap config.xml:

    <access origin="*" />

我在Chrome中进行了所有的phonegap开发,并且实施了跨域安全性.一旦您尝试针对服务器的真实地址(而不是localhost)进行测试,Chrome就会阻止它,除非为服务器设置了CORS.在服务器上的PHP中启用此功能以进行测试的简单方法是:

I do all my phonegap development in Chrome and it enforces Cross-Origin security. Once you try to test against your server's real address (instead of localhost) Chrome will block it unless the server is set up for CORS. Easy way to enable this for testing in PHP on your server is:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type');