Posted By

dropthenerd on 02/02/11


Tagged

ajax


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

cplusplus
virendrakryadav
czehner
qrczak
wirenaught
jillella
A973C
catrinho
lookingforsome
Priestd09
unibox


Simple Ajax Example


 / Published in: jQuery
 

URL: http://www.dropthenerd.com/ajax-a-simple-approach-that-you-will-use-and-reuse/

  1. $('.link').click(function(){
  2.  
  3. var params = {'answer' : $(this).attr('answer')};
  4. var href = $(this).attr('href');
  5.  
  6. $.post(
  7. url: href,
  8. data :params,
  9. success: function(data){
  10. switch(data.success){
  11. case 1: //success
  12. alert('Correct Response: ' + data.message);
  13. break;
  14.  
  15. case 0: //failure
  16. alert('Incorrect Response: ' + data.message);
  17. break;
  18.  
  19. default: //anything else - in this case, an error.
  20. alert('Error! ' + data.message);
  21. break;
  22.  
  23. }
  24. },
  25. dataType: 'json'
  26. );
  27.  
  28. return false;
  29. });

Report this snippet  

You need to login to post a comment.