Created
September 10, 2013 14:24
-
-
Save markmichon/6510134 to your computer and use it in GitHub Desktop.
The perfect Ajax Request http://kyleschaeffer.com/development/the-perfect-jquery-ajax-request/
This file contains hidden or 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
$.ajax({ | |
type: 'POST', | |
url: 'http://kyleschaeffer.com/feed/', | |
data: { postVar1: 'theValue1', postVar2: 'theValue2' }, | |
beforeSend:function(){ | |
// this is where we append a loading image | |
$('#ajax-panel').html('<div class="loading"><img src="/images/loading.gif" alt="Loading..." /></div>'); | |
}, | |
success:function(data){ | |
// successful request; do something with the data | |
$('#ajax-panel').empty(); | |
$(data).find('item').each(function(i){ | |
$('#ajax-panel').append('<h4>' + $(this).find('title').text() + '</h4><p>' + $(this).find('link').text() + '</p>'); | |
}); | |
}, | |
error:function(){ | |
// failed request; give feedback to user | |
$('#ajax-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks buddy. Kyles site was down today and I copied code from here.