sashomaga

Http requests pattern

Jun 30th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var httpRequester = (function () { //success error -> callbacks
  2.     function getJson(url, success, error) {
  3.         $.ajax({
  4.             url: url,
  5.             type: "GET",
  6.             contentType: "application/json",
  7.             timeout: 5000,
  8.             success: success,
  9.             error : error
  10.         })
  11.     }
  12.  
  13.     function postJson(url, data, success, error) {
  14.         $.ajax({
  15.             url: url,
  16.             type: "POST",
  17.             contentType: "application/json",
  18.             timeout: 5000,
  19.             data: JSON.stringify(data),
  20.             success: success,
  21.             error: error
  22.         })
  23.     }
  24.  
  25.     return {
  26.         getJson: getJson,
  27.         postJson: postJson
  28.     }
  29. }());
  30.  
  31. var user =
  32. {
  33.     "username": "Dodo",
  34.     "authCode": "6fa9133efe05348e430bd5a4585b595f0cb6cba3"
  35. }
  36.  
  37. httpRequester.postJson("http://localhost:40643/api/user/login", user, function () { alert("success") }, function () { alert("error") });
Advertisement
Add Comment
Please, Sign In to add comment