使用Ajax Promises / Deferred

时间:2016-05-08 09:31:00

标签: javascript jquery ajax promise deferred

我正在尝试使用以下代码获取Ajax promise。因为我的函数在启动实际调用之前进行了另一个ajax调用,为了获得authKey,实际调用中的promise (应该已经返回)为空,&我不能使用.then(),因为我认为我没有得到任何回报。我不知道为什么。

我在这里做错了什么?有没有其他方法可以解决这个问题。我调用getAjaxPromise(),如下所述,但返回null:

   getAjaxPromise(myUrl, true, myType, myContentType, mySuccessFunction, myFailureFunction, 
myData, true)
.then(function(data) //.then() gives undefined-null error
      {
        //Do something with the data returned form actual Ajax call.
      });

self.getAjaxPromise = function(url, async, type, contentType, successCallback, 
errorCallback, data, isSecureCall) 
{
  if (isSecureCall) {
    var tokenPromise = getTokenPromiseFromServer(); //Another Ajax call to get latest token from service
    tokenPromise.then(function(tokenData) {  //This then runs fine
      return $.ajax({
        beforeSend: function(request) {
          request.setRequestHeader("authKey", tokenData.key);
        },
        url: url,
        async: async,
        type: type,
        contentType: contentType,
        success: successCallback, //Success callback runs fine, then() does not
        error: errorCallback, //Error callback runs fine, then() does not
        data: JSON.stringify(data)
      });
    });
  } else { //Just one ajax call 
    return $.ajax({
      beforeSend: function(request) {
        request.setRequestHeader("authKey", "anonymous");
      },
      url: url,
      async: async,
      type: type,
      contentType: contentType,
      success: successCallback,
      error: errorCallback,
      data: JSON.stringify(data)
    });
  });
}
};

3 个答案:

答案 0 :(得分:2)

您忘记返回getTokenPromiseFromServer
如果isSecureCall为true,则函数返回null

self.getAjaxPromise = function(url, async, type, contentType, successCallback, 
errorCallback, data, isSecureCall) 
{
  if (isSecureCall) {
    return getTokenPromiseFromServer().then(function(tokenData) {
      return $.ajax({
        beforeSend: function(request) {
          request.setRequestHeader("authKey", tokenData.key);
        },
        url: url,
        async: async,
        type: type,
        contentType: contentType,
        success: successCallback, //Success callback runs fine, then() does not
        error: errorCallback, //Error callback runs fine, then() does not
        data: JSON.stringify(data)
      });
    });
  } else { //Just one ajax call 
    return $.ajax({
      beforeSend: function(request) {
        request.setRequestHeader("authKey", "anonymous");
      },
      url: url,
      async: async,
      type: type,
      contentType: contentType,
      success: successCallback,
      error: errorCallback,
      data: JSON.stringify(data)
    });
  });
}
};

答案 1 :(得分:1)

您忘记在if语句中返回promise,只返回else,下面的固定代码:

self.getAjaxPromise = function(url, async, type, contentType, successCallback,
  errorCallback, data, isSecureCall) {
  if (isSecureCall) {
    var tokenPromise = getTokenPromiseFromServer(); //Another Ajax call to get latest token from service
    tokenPromise.then(function(tokenData) {
      return $.ajax({
        beforeSend: function(request) {
          request.setRequestHeader("authKey", tokenData.key);
        },
        url: url,
        async: async,
        type: type,
        contentType: contentType,
        success: successCallback, //Success callback runs fine, then() does not
        error: errorCallback, //Error callback runs fine, then() does not
        data: JSON.stringify(data)
      });
    });

    return tokenPromise;
  } else { //Just one ajax call 
    return $.ajax({
      beforeSend: function(request) {
        request.setRequestHeader("authKey", "anonymous");
      },
      url: url,
      async: async,
      type: type,
      contentType: contentType,
      success: successCallback,
      error: errorCallback,
      data: JSON.stringify(data)
    });
  });
}
};

答案 2 :(得分:1)

您忘记返回tokenPromise 如果

,你必须从头开始
if (isSecureCall) {
    var tokenPromise = getTokenPromiseFromServer(); //Another Ajax call to get latest token from service

    // ...

    return tokenPromise;
  }