Programming Tips - jQuery: Robust AJAX use

Date: 2017sep25 Update: 2025sep4 Language: javaScript Library: jQuery Q. jQuery: Robust AJAX use A. I do not use the post() or get() methods because they don't have an error callback. Use the ajax() method is a bit more verbose but has more options - including an error callback. Example Use:
function getSomeInfoFromServer(urlOnServer) { $.ajax({ type: 'get', // We are using the GET method url: urlOnServer, // You make this page dataType: 'json', // Its returning JSON cache: false, // Don't cache the result success: function(data, textStatus, jqXHR) { // Do something with data $('#status').html('done'); }, error: function(data, textStatus, jqXHR) { $('#status').html(textStatus); } }); }
Expects HTML:
<div id=status></div>
Example Use:
$(document).ready(function() { getSomeInfoFromServer('get_some_info.php'); });