0

I am new to HTML 5. I am developing a phone gap application. In that my web services and database is on server and HTML pages are on device which is compiled through phone gap library.

When I execute HTML pages on browser I am getting proper output on browser but when I compile project through phone gap then and launch application on device there is no response. I traced the flow using alert messages in that i found that AJAX call to webservice is not working in device.

Can any one please tell me what to do in this case?

following is the code that I am using to call 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>
Shital Tiwari
  • 175
  • 2
  • 3
  • 17
  • 1
    Well, there is no AJAX here currently; you'd have to create an AJAX object first. – MackieeE Jan 31 '14 at 10:13
  • so you create a – Alex Tape Jan 31 '14 at 10:13
  • Do you have an issue posting back to the link from the device probably because they are not in the same subnet? I see you have used a private IP on your url. Is you device registered on the network? – Shouvik Jan 31 '14 at 10:14
  • This is not the proper ajax call Shital. you need to use $.ajax or $.getJSON to call webservice from phonegap – Bhavesh Parekh Jan 31 '14 at 10:14
  • @BhaveshParekh she also needs to use jquery then. Don't see a reference of that on the code. – Shouvik Jan 31 '14 at 10:15
  • Then use httprequest for webservice calling. – Bhavesh Parekh Jan 31 '14 at 10:30
  • You can check on this link [http://g-b-log.blogspot.in/2010/05/call-web-service-from-javascript.html] – Bhavesh Parekh Jan 31 '14 at 10:38
  • did you configure access origin in config.xml to allow access to your server? – QuickFix Feb 02 '14 at 23:16

1 Answers1

0

You could try changing your code to look more like this and putting it into a JavaScript file:

    $(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.');
        }
    });
   });
TLama
  • 75,147
  • 17
  • 214
  • 392
ejwill
  • 141
  • 8