Tuesday, July 31, 2012

Load JSON Data Using Ajax


<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>

<script type="text/javascript" >

$( '#page1' ).live( 'pageshow',function(event, ui){
  getData();
});

var liststr= "";
function getData() {                     
    $.ajax({                    
           url: "jsondata.php",
           dataType: 'jsonp',
           timeout: 9000,
           success: function(data, status) {
 $.each(data.movies, function(i, item) {                            
                   liststr+='<br>title '+item.title+'<br>year '+item.year+'  <br>description '+item.synopsis+'<br>';
                });    
                 $('#moviecontent').html(liststr).fadeIn();  
        },
           error: function() {
           var error = confirm('There was an error loading the data. Try loading the data again.');
         
           if (error){  
            alert(error);                    
           }                  
        }
    });                  
}
</script>

</head>
<body>

<div data-role="page" id="page1">
<div data-role="header">
<h1>Page 1</h1>
</div>
<div data-role="content">
<p>Page1 content goes here. Its a Page1.</p>
<div id="moviecontent"></div>
<a href="#page2" data-role="button" >Page2</a>
</div>
<div data-role="footer">
<h4>Page1 Footer</h4>
</div>
</div>
</body>
</html>

Cheers..

No comments:

Post a Comment