Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. app.post('/login', function (req, res) {
  2. var username = req.body.username; // etc...
  3. if (username === "George" && password === "cat") {
  4. res.json({message : "success"});
  5. } else {
  6. res.json({message : "fail"});
  7. }
  8. });
  9.  
  10. app.post('/login', function (req, res) {
  11. var username = req.body.username; // etc...
  12. if (username === "George" && password === "cat") {
  13. res.json({status: 200, message : "success"});
  14. }
  15. else {
  16. res.json({status: 401, message : "fail"});
  17. }
  18. });
  19.  
  20. $.post( "/login", function(loginData) {
  21.  
  22. })
  23. .done(function(data) {
  24. if(data.status === 200) {
  25. //logged in
  26. } else {
  27. //logged out
  28. }
  29. })
  30. .fail(function() {
  31. //error (bad connection)
  32. });
  33.  
  34. $('#some-button').on('click', function() {
  35. //lets go make a request with some ajax call
  36.  
  37. // lets say ajax call fails and responds with something like this:
  38. var error = {
  39. message: 'Could not retrieve data.',
  40. code: 403
  41. };
  42.  
  43. //We can build a string of html, add the error fields to the html, and add that html to our document like so.
  44. var errorHtml = "<div class='error'><h3>Sorry, an error has occured.</h3><p>Message: _message</p><p>Code: _code</p></div>"
  45. .replace('_message', error.message)
  46. .replace('_code', error.code)
  47.  
  48. // Add to document
  49. $('#error').html(errorHtml)
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement