简体   繁体   English

您可以从xhttp请求返回值吗?

[英]Can you return a value from a xhttp request?

I have an xhttp function that calls my db and returns an array, the array is different depending on the parameter passed in the xhttp function when called. 我有一个xhttp函数,该函数调用我的数据库并返回一个数组,该数组因调用时在xhttp函数中传递的参数而异。 This is the xhttp function I have: 这是我拥有的xhttp函数:

fetchGroupInfo: function (groupNum) {
                var global = this;

                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {                                                         //(callback function)this function runs after xhttp.open because we have asynchronous data sending so as soon as the data is recieved this is run
                    if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status == 200) {
                        console.log(this.responseText);
                        //I parse the rawdata received into jason structure then I pass it into and call the manipulate function
                        var rawdata = this.responseText;
                        var json = JSON.parse(rawdata); //parses the query result
                        return json;
                    }
                };

                xhttp.open("GET", "http://178.62.***.***:1020/groupInfo/"+groupNum, true);
                xhttp.send();
            },

Since I need to call the function 8 times on page load and get back 8 different arrays, I do not want to have to write out this function 8 times for each different array I need to get back. 由于我需要在页面加载时调用该函数8次并取回8个不同的数组,因此我不想为需要返回的每个不同数组都将该函数写出8次。 what I would like to be able to do later is something like this, so that I can keep my code clean: 我以后想要做的是这样的,这样我就可以保持代码干净:

this.group1Info = this.fetchGroupInfo(1);
this.group2Info = this.fetchGroupInfo(2);
this.group3Info = this.fetchGroupInfo(3);
this.group4Info = this.fetchGroupInfo(4);
.....

At the moment the way that the function is set up it is returning an undefined value, how would I make this work? 目前,该函数的设置方式正在返回一个未定义的值,我将如何进行这项工作?

Your code is asynchronous, so you'll you to add a callback function and then set your variables in the callback: 您的代码是异步的,因此您将添加一个回调函数,然后在回调中设置变量:

fetchGroupInfo: function (groupNum, callback) {
            var global = this;

            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {                                                         //(callback function)this function runs after xhttp.open because we have asynchronous data sending so as soon as the data is recieved this is run
                if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status == 200) {
                    console.log(this.responseText);
                    //I parse the rawdata received into jason structure then I pass it into and call the manipulate function
                    var rawdata = this.responseText;
                    var json = JSON.parse(rawdata); //parses the query result
                    return callback(json);
                }
            };

            xhttp.open("GET", "http://178.62.***.***:1020/groupInfo/"+groupNum, true);
            xhttp.send();
        }

and now to set the variables, pass a callback each time you call fetchGroupInfo : 现在要设置变量,每次调用fetchGroupInfo传递一个回调:

this.fetchGroupInfo(1, function(result) {
  this.group1Info = result;
});

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

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