Saturday, August 10, 2013

Javascript is great to write cross platform client side scripts. With JQuery and Ajax, web requests are even more easier to write. Here is an example.
$.ajax({
 type: 'POST',
 url: 'http://openapi.starbucks.com',
data: { param1 : 'value1' },
beforeSend: function(){
 $('#ajax-panel').html('<div class="loading"><img src="/images/loading.gif" alt="Loading..." /></div>');
},
success:function(data){
$('#ajax-panel').empty();
$(data).find('item').each(function (i) {
$('#ajax-panel').append('<h4>' + $(this).find('title').text()+ '</h4>');
});
},
error:function(){
$('#ajax-panel').html('<p class="error"> There was an error</p>');
}
});
or we could make an asynchronous request:
$.ajax({
   type: "POST",
   async: true,
   url: "http://<myserver>/<resource>",
   data: "{" + args + "}",
   contentType: "application/json"
   dataType: "json",
   success: function show(result) {
    data = result.d;
   },
   fail: function err(xhr) {
   data = "ERROR";
   }
});

No comments:

Post a Comment