简体   繁体   中英

How to get values of a ajax response

In my Javascript file, I get datas from server with an Ajax call :

this.$http.get("/data?startDate="+this.filtres.startDate+" "+this.filtres.startHour+"&endDate="+this.filtres.endDate+" "+this.filtres.endHour).then(function(response) {
    this.todos = response.body;
    this.$forceUpdate();
}); 

response.body looks like this : 在此处输入图片说明

(1) [...]
    0: Object { nbPieces: Getter & Setter, TRE: Getter & Setter, TRS: Getter & Setter, ... }
    __ob__: {…}
​        dep: Object { id: 35, subs: [] }​​
        value: Array [ {…} ]
        vmCount: 0​​
    <prototype>: Object { walk: walk(), observeArray: observeArray(), … }
    length: 1

Now I would like to put value of nbPieces, TRE and TRS into variables. I tried by doing response.body.values() but I doesn't work.

Your response.body is an array with an object. If it's always going to have length of 1 then this will work for you:

const [{ nbPieces, TRE, TRS }] = response.body;

and then you can use it like normal variables:

console.log(nbPieces, TRE, TRS);

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