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

时间:2015-10-13 08:24:22

标签: javascript ajax dojo

当我回来时,我遇到了一些问题"结果"在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;
            }
        );
    },

所以,在第二个代码中,我无法得到结果,结果是"未定义"。请帮助我,我是一个新手道场。

谢谢!

2 个答案:

答案 0 :(得分:1)

如果你那样测试怎么办?

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;
            })
            */
        );
    },

答案 1 :(得分:0)

在第一个代码中,我将使用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;
    },