How to connect to a PHP file on a web server using PhoneGap (Android app) in real time?

The code below works fine using the wamp server on localhost. It calls the php file, which connects to the MySql database and returns the data.

However, I am trying to create a mobileapp using PhoneGap. The code below is in the HTML file. My questions are: how will my html file establish a connection to the server using ajax as soon as I load it into the phone records and generate the .apk file. Since the code below simply calls the getbustime.php file without any web server settings.

I developed my phone recording application using HTML, jQueryMobile, Ajax and now want to download 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>
+3
1

, server/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:

    <access origin="*" />

  • Chrome Cross-Origin. ( localhost), Chrome , CORS. PHP :

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

+2

All Articles