使用 Ajax 个 Promises / Deferred

Working with Ajax Promises / Deferred

我正在尝试使用以下代码获取 Ajax promise。因为我的函数在启动实际调用之前进行了另一个 ajax 调用,为了获得 authKey,承诺 (应该 returned)来自实际调用的是空的,&我不能在它上面使用 .then() 因为我认为我没有从它的 return 中得到任何东西。我不确定为什么。

我在这里做错了什么?有没有其他方法可以解决这个问题。我像下面提到的那样调用 getAjaxPromise() 但在 return:

中得到 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)
    });
  });
}
};

您忘记了 return getTokenPromiseFromServer
如果 isSecureCall 为 true 你的函数 return 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)
    });
  });
}
};

你忘记了returnif语句里面的promise,你只是return在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)
    });
  });
}
};

您忘记了return tokenPromise 你必须 return 从一开始就 if

if (isSecureCall) {
    var tokenPromise = getTokenPromiseFromServer(); //Another Ajax call to get latest token from service

    // ...

    return tokenPromise;
  }