Simple Ajax Example


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  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. });

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.