title: How to re try operation after refresh token - jquery ajax
body: <p>In my flow say i am using an access token for getting my data. When my access token expires i get a 401 error i am using the refresh token to get a new access token. </p>
  
  <p>Note : My access token and refresh token is stored in a cookie and i am updating the same after a 401 error.</p>
  
  <p><strong>My question how do i retry the same operation which i was in the middle of?</strong></p>
  
  <p>My Code (services.js):</p>
  
  <pre><code> var refresh_token = "na";
  function get_api_data(url, api_token) {
      var returnData = handleApiData(url, api_token, "GET");
      return returnData;
  }
  
  function post_api_data(url, api_token, post_data) {
      var returnData = handleApiData(url, api_token, "PUT", post_data);
      return returnData;
  
  }
  
  function handleApiData(url, access_token, type, post_data) {
      return $.ajax({
          url: url,
          type: type,
          data: post_data,
          error: failHandler,
          contentType: "application/json",
          beforeSend: function (xhr) {
              xhr.setRequestHeader("Authorization", "Bearer " + access_token);
          }
      })
  }
  
  function handleData(data, textStatus, jqXHR) {
      return data;
  }
  
  function failHandler(jqXHR, textStatus, errorThrown) {
      switch (jqXHR.status) {
          case 401:
              var api = get_api_token();
              checkApiToken(api.refresh_token);
              break;
          default:
              alert(errorThrown);
  
  
      }
  }
  function checkApiToken(refresh_token) {
      if (refresh_token != "na") {
          $.post("/Account/Refresh/?refresh_token=" + refresh_token);
          //location.reload();
      }
  }
  </code></pre>
  
  <p>My Code (notification.js):</p>
  
  <pre><code> $(function () {
      var api = get_api_token();
      if (api != null)
          get_notification_data(api.access_token);
  
  });
  
  function get_notification_data(api_token) {
  
      var notifications = get_api_data(urls.notifications.list, api_token);
      if (notifications != undefined)
          notifications.success(function (data) {
              items = data.records;
              _.each(items, function (item) {
                  item.Status = ko.observable(item.status);
                  item.onClick = function () {
                      if (item.Status() === 'UNREAD') {
                          var post_data = { id: item.id };
                          post_api_data(urls.notifications.list, api_token, post_data).success(function (response, textStatus) {
                              if (response.success)
                                  item.Status('READ');
                              $(location).attr("href", item.action_link);
                          });
                      }
                      else {
                          $(location).attr("href", item.action_link);
                      }
  
                  }
              });
              var model = {
                  items: ko.observableArray(items),
                  onCancel: function (item) {
                  }
              }
              ko.applyBindings(model, $("#notificationBar")[0]);
          })
  }
  </code></pre>
  
  <p>Edit: My AccountController code that sets the new API cookie:</p>
  
  <pre><code> [HttpPost]
      public ActionResult Refresh(string refresh_token)
      {
          string token_string = string.Empty;
          try
          {
              token_string = OAuthHelper.getTokenViaRefreshTokenFromAPIServer(refresh_token);
              if(token_string != null)
              Response.Cookies[Constants.Cookies.API].Value = token_string;
          }
          catch (Exception ex)
          {
              Log.Info(string.Format("AccountController.cs -Refresh Token Error ", ex.Message));
  
          }
         return RedirectToAction("Index","Home");
      }
  </code></pre>