使用Ajax Promises / Deferred

我正在尝试使用下面的代码获得Ajax promise 。 因为我的函数在启动实际的函数之前进行另一个ajax调用,为了获取authKey ,实际调用的promise (应该已经返回)为null,我不能使用.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) }); }); } }; 

你忘了返回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) }); }); } }; 

您忘记在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) }); }); } }; 

你忘了返回tokenPromise你必须先从if返回它

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