我正在尝试使用 jQuery 的延迟功能。我想要做的是调用一个 GetToken 函数(它验证用户名和密码并返回一个令牌),然后调用另一个函数,将令牌作为第一个参数传递......然后是其他几个参数。我看到的所有示例都假设第二个函数的唯一参数将是第一个函数的响应。
var GetToken = function (username, password) {
return $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: webService + "ccGetToken",
data: "{UserId: '" + username + "', UserPass: '" + password + "'}",
dataType: "json"
});}
var SimpleQuery = function(token, docTypeID, aTypeID, Value){
return $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: webService + "ccSimpleQuery",
data: "{UserToken: '" + token + "', DocTypeID: '" + docTypeID + "', AType: '" + aTypeID + "', ccAValue: '" + Value + "'}",
dataType: "json"
});}
像这样的东西?
$.when(GetToken("username", "password")).pipe(SimpleQuery(response.d, "DocTypeId", "ATypeId", "Value")).then(function(response){//do stuff here});