我有一些问题,当我返回"结果"在xhr.then()和我使用dojo 1.10.4请帮助我!
下面的代码连接API与xhr ('dojo/request/xhr')在dojo:
remandFlow: function(postData, flowId) {
return xhr(this.REMAND_URL + flowId, {
method: 'POST',
data: postData,
handleAs: 'json',
headers: {'X-CSRFToken': cookie("csrftoken")}
}).then(
lang.hitch(this, function(result){
return result;
}),
lang.hitch(this, function(error){
return error;
})
);
},
下面的代码得到上面代码的结果:
editStepUser: function(stepComponent, routeComponent) {
this.model.remandFlow(postData).then(
function(result){
console.log(result) //I can not get it, It's undefined
result.targeted_step_id = postData.route_step_id;
},
function(result){
result.targeted_step_id = postData.route_step_id;
}
);
},
所以,在第二个代码中,我不能得到结果,结果是"未定义"。请帮帮我,我是道场新手。
谢谢! !
如果你这样测试会发生什么?
remandFlow: function(postData, flowId) {
// edit : added console.log
console.log('remandFlow : ' , postData , '|' , flowId);
console.log('remandFlow => this.REMAND_URL : ' , this.REMAND_URL);
return xhr(this.REMAND_URL + flowId, {
method: 'POST',
data: postData,
handleAs: 'json',
headers: {'X-CSRFToken': cookie("csrftoken")}
}).then(
function( result ){ return result; } ,
function( error ){ return error; }
/*
lang.hitch(this, function(result){
return result;
}),
lang.hitch(this, function(error){
return error;
})
*/
);
},
在第一个代码中,我将使用deferred来返回deferred.promise。
declare(['dojo/Deferred',.....], function(Deferred,....){
remandFlow: function(postData, flowId) {
var deferred = new Deferred();
xhr(this.REMAND_URL + flowId, {
method: 'POST',
data: postData,
handleAs: 'json',
headers: {'X-CSRFToken': cookie("csrftoken")}
}).then(
lang.hitch(this, function(result){
deferred.resolve(result);
}),
lang.hitch(this, function(error){
deferred.reject(error);
})
);
return deferred.promise;
},