简体   繁体   中英

Unable to parse JSON output in jquarymobile's html page using Ajax

I have developed screens for my android application using PhoneGap and jQuery Mobile. Using below js and css files in my html page

 <link rel="stylesheet" href="css/jquery.mobile-1.3.1.css" />
 <script src="js/jquery-1.9.1.js" type="text/javascript"></script>
 <script src="js/jquery.mobile-1.3.1.js" type="text/javascript"></script>

Now, Screens are working fine and I need to bind data from db to my screen for that I have created web services that are giving me JSON outputs. One of the JSON outputs that I'm trying to parse in html is as below.

[
 {"rest_id": 35, "rest_name": "Just 10"},
 {"rest_id": 36, "rest_name": "Egg Zone"},
 {"rest_id": 37, "rest_name": "Tandoor & Curries"},
 {"rest_id": 38, "rest_name": "China Bite"}
]

Now, to my HTML Page. What I'm trying here is this

<script type="text/javascript">
        $.getJSON("MyURL", function( data ) {
            alert('success');
            /* My Logic for getting value from JSON output */
        });
     </script>

Issue here is I'm not even getting alert msg that I'm trying to print on the page.

I'm new to all these .. So if any1 can tell me where and what I'm doing wrong would be great help.

Thanks all

Mayur

Well, I am not familiar with PhoneGap but it seems in your script you have written code to get data. There should be something about request too. As Ajax is about request and response.

  $.ajax({
  url : '${resourceURL}',
  data : data,//send Request 
  type: 'POST',
  dataType : "json",
  success : function(data) {
    // something my be alert(success)
  }
});

From the code you provided it seems you are not calling that $.getJSON() function. You need to call it somehow (from some event listener for example) or you can execute it when DOM is ready by

$(function(){
  $.getJSON("MyURL",
    function(data){
      alert('success');
      /* My Logic for getting value from JSON output */
    }
  );
});

just to see if it is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM