如何使在实时使用的PhoneGap(Andriod的应用程序)的网络服务器到PHP文件的连接?应用程序、实时、服务器、文件

2023-09-10 19:26:33 作者:气场征服一切

下面给出的code工作正常使用的localhost.It一个瓦帕服务器调用连接到MySQL数据库,并返回数据的PHP文件。

不过,我想建立使用PhoneGap的一个mobileapp。下面code是一个HTML文件。我的问题是如何将我的HTML文件进行与服务器的连接使用AJAX一旦我把它上传到PhoneGap的和生成的.apk文件。由于低于code只是调用getbustime.php文件,而无需任何Web服务器的参数。

我已经devloped我在PhoneGap的应用程序中使用HTML,jQueryMobile,Ajax和现在愿意上传的帮助?

 函数getBusTime(){



                         如果(window.XMLHtt prequest)
                        {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
                        XMLHTTP =新XMLHtt prequest();
                        }
                      其他
                        {// code对IE6,IE5
                        XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
                        }
                      xmlhttp.onreadystatechange =功能()
                        {
                        如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
                          {

                         的document.getElementById(resultLogBus)的innerHTML = xmlhttp.responseText。

                    // $('#resultLogBus)HTML(xmlhttp.responseText).selectme​​nu(刷新)。
                         }
                        }

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



    } //结束功能getBusTime的


< / SCRIPT>
 

解决方案

基本上,你需要设置一个完整路径的服务器/ php文件在你的Ajax调用像这样:

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

您可能还需要根据您的情况做一对夫妇的其他事情是:

设置在PhoneGap的config.xml文件本文标签: &LT;获得原产地=*/&GT;

我在Chrome浏览器的所有我的PhoneGap开发并实施跨域安全。一旦你尝试测试对您的服务器的真实地址(而不是本地主机)Chrome浏览器会阻止它,除非服务器设置为CORS。简单的方法来启用该测试在PHP服务器上的:

头(访问控制 - 允许 - 产地:*'); 头(访问控制 - 允许 - 头:X-请求,通过,内容类型');

Andriod Pad上的应用

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.

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.

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>

解决方案

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:

set this tag in the phonegap config.xml: <access origin="*" />

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');