Search code examples
iphonecordovaios4

How to get JSON response from the web services URL in PhoneGap?


I got stuck with an issue in iOS application using PhoneGap framework. I have a web services URL. I need to get JSON response from the web services URL. I had build up some code, but it is not working.

Here is my code:

<div data-role="content" data-theme="a" style="background: Black">
    <div data-theme="a">
        <span style="font-size: x-large; color: Orange;">Secure Log In</span></div>
    <div data-theme="a">
        <div data-theme="a">
            <input type="password" placeholder="PASSWORD" id="txtPassword" style="background-color: gray;" /></div>
     <div data-theme="a" align="right">
        <a href="#" data-role="button" onclick="callWebService()" data-corners="false"
                data-theme="a" id="clcik" cursor="pointer" style="width: 150px; border-radius: 5px 5px 5px 5px"
                data-clickload="show" data-transition="slidefade"><span style="color: Green">Log In</span>
            </a>
        </div>

        function callWebService(){

            var query = 'Ocean';
            var url = 'http://66.171.142.16/Accountservice/Security/ValidateAccess?accesscode=abcd&type=1';
            alert(url);

            $.getJSON(url,function(response){
                      alert('Here!');
                      });

        };

How can I get the JSON response from the url?


Solution

  • I used .Net web Service to access Web Service, Also I have created a Plugin to call .Net web Service. in Java script I used to call web service method as described below.

    in script.js

       $(".CategoryNavbar").click(function(e){
                         e.preventDefault();
                          window.plugins.webservice.GetFlights("service",function(r){printResult(r)},function(e){console.log(e)});
                         return false;  
                     });
    
    
      function printResult(fileInfo){
    
    
                    var innerHtmlText=getHtml(fileInfo);  
    
                      $.mobile.changePage('#CategoryPage',{transition:'slide'});
    
                        $('#CategoryPageContent').html(innerHtmlText);
    
                        $("#CategoryList").listview();
                        $("#CategoryList").listview('refresh');
    
    
    
                }
    
    
    
    
      function getHtml(fileInfo){
                     var htmlText='<ul data-role="listview" id="CategoryList" data-theme="c" data-filter="true" data-filter-placeholder="Search">';
    
                     for(var index=0;index<fileInfo.Flights.length;index++){
                         htmlText=htmlText+'<li> <a href="">'+ fileInfo.Flights[index] +'</a></li>';
                         }
    
                     htmlText=htmlText+"</ul>";
                     return htmlText;
    
                     } 
    

    in Plugin File /** * Constructor */ function WebService() { }

    /** * @param methodn The method name for which we want the webService * @param successCallback The callback which will be called when directory listing is successful * @param failureCallback The callback which will be called when directory listing encouters an error */

    WebService.prototype.GetFlights = function(args, successCallback,
            failureCallback) {
        return cordova.exec(successCallback, failureCallback, 'WebService',
                'GetFlights', [ args ]);
    };
    
    if (!window.plugins) {
        window.plugins = {};
    }
    if (!window.plugins.webservice) {
        window.plugins.webservice = new WebService();
    }