简体   繁体   English

使用Ajax承诺/推迟

[英]Working with Ajax Promises / Deferred

I am trying to get an Ajax promise using the code below. 我正在尝试使用下面的代码获得Ajax promise Because my function makes another ajax call before initiating the actual one , for getting the authKey , The promise (that should have been returned) from the actual call is null, & I cannot use .then() on it because I think I am not getting anything in return from it. 因为我的函数在启动实际的ajax调用之前进行了另一个ajax调用,以获取authKey ,所以实际调用中的promise (应该已经返回)为null,并且我无法在其上使用.then() ,因为我认为我不是得到任何回报。 I am not sure why. 我不知道为什么。

What am I doing wrong here? 我在这里做错了什么? Is there any other way to go about this. 还有其他方法可以解决此问题。 I call getAjaxPromise() like mentioned below but get null in return: 我像下面提到的那样调用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) }); }); } }; 

you forgot to return the getTokenPromiseFromServer 您忘记返回getTokenPromiseFromServer
if isSecureCall is true your function return null 如果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) }); }); } }; 

You had forgot to return the promise inside the if statement, you are return it only on else, the fixed code below: 您忘记了在if语句中返回诺言,而仅在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) }); }); } }; 

You forgot to return tokenPromise you must return it from first if 您忘记了返回令牌承诺,如果您必须从头开始将其返回

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

    // ...

    return tokenPromise;
  } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM