Created
January 13, 2015 20:39
-
-
Save namlet/48dc789425b3e0b10d7b to your computer and use it in GitHub Desktop.
Standard Ajax Call Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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