简体   繁体   中英

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

I have some problems when I return "result" in xhr.then() and I use dojo 1.10.4 Pls help me!

The below code which connects to API with xhr ('dojo/request/xhr') in 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;
            })
        );
    },

The below code which gets result of above code:

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

So, in the second code, I can't get result, result is "undefined". Please help me, I'm a newbie dojo.

Thanks!!

What happen if you test like that ??

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

In the first code, I will use deferred to return a deferred.promise. This's my way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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