繁体   English   中英

无法在xhr.then中返回“结果”('dojo / request / xhr')

[英]Can not return “result” in xhr.then ('dojo/request/xhr')

当我在xhr.then()中返回“结果”并且使用dojo 1.10.4时,我遇到了一些问题!

以下代码通过dojo中的xhr('dojo / request / xhr')连接到API:

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;
    },

暂无
暂无

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

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