簡體   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