erdogany
8/1/2013 - 12:47 PM

Basic JQuery ajax functions

Basic JQuery ajax functions

// GET Method
// Load data from the server using a HTTP GET request.
// Reference:
jQuery.get( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] );
Returns: jqXHR

// Example:
$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

// POST Method
// Reference:
jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] );
Returns: jqXHR

// Example:
$.post('ajax/test.html', function(data) {
  $('.result').html(data);
});

// GET JSON
// Load JSON-encoded data from the server using a GET HTTP request.
// Reference:
jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] );
Returns: jqXHR

// Example:
$.getJSON('ajax/test.json', function(data) {
  var items = [];
 
  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });
 
  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});