Skip to content

Instantly share code, notes, and snippets.

@namlet
Created January 13, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namlet/48dc789425b3e0b10d7b to your computer and use it in GitHub Desktop.
Save namlet/48dc789425b3e0b10d7b to your computer and use it in GitHub Desktop.
Standard Ajax Call Setup
// Setup default behavior (in some init script probably)
$(document).ajaxError(function( event, jqxhr, settings, thrownError ) {
console.log('Ajax Error: ' + thrownError);
console.log('Ajax Error jqXHR: ' + jqxhr);
console.log('Ajax settings: ' + settings);
});
$.ajaxSetup({
dataType: json,
type: 'POST'
});
// Standard usage
var some_data = {
name: 'Billy Bob',
tendencies: 'Disproportionate musical rage'
}
// POST
$.ajax({
url: '/user/' + user_id + '/change-preferences',
data: some_data
}).done(function( results ) {
// do something with results
console.log(results);
});
// GET
$.get('/user/' + user_id + '/change-preferences', some_data, function(results){
// do something with results
console.log(results);
});
// OR GET using $.ajax
$.ajax({
url: '/user/' + user_id + '/change-preferences',
data: some_data,
type: 'GET'
}).done(function( results ) {
// do something with results
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment