jQuery技术:使用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返回它

    需要了解更多jQuery教程分享使用Ajax Promises / Deferred,都可以关注jQuery技术分享栏目—计算机技术网(www.ctvol.com)!

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

      以上就是jQuery教程分享使用Ajax Promises / Deferred相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/982115.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐