Overview
I’ve been moving into using AJAX as a way to create applications directly in websites. While researching, I’ve come across a number of methods that help me retrieve data from and set data on the server for the client. This article strictly covers retrieval methods.
jQuery
Code snippet #1 is the shortcut call for retrieving data from the server. If you want to make a distinction between complete, error, and success events, you would have to use the more traditional AJAX call in code snippet #2.
Code Snippet #1
jQuery.get( 'example.json', { key1: value1, .. }, function( data ) { // This is success. } );
Code Snippet #2
jQuery.ajax( {
type: 'GET',
url: 'example.json',
data: { key1: value1, .. },
completed: function() { .. },
success: function() { .. }
});
AngularJS
Code snippet #3 is the shortcut call for retrieving data from the server. If you wanted to pass additional data along with your request, notice the use of the ‘params’ key in code snippet #4. This part really does not match up well with jQuery’s version and took me quite a bit of time to come across the answer – it was not clear in the docs.
Code Snippet #3
$http.get(
'example.json',
{ key1: value1, .. }
)
.success( function( response ) {
})
.error( function() {
});
Code Snippet #4
$http({
method: 'GET',
url: 'example.json',
params: { key1: value1, .. }
})
.success( function( response ) {
})
.error( function() {
});