Trying to access response data using fetch

后端 未结 2 1052
鱼传尺愫
鱼传尺愫 2021-02-07 21:12

I\'m trying something simple where I make a request from the front end of my app using the fetch API like so

let request = new Request(\'http://localhost:3000/a         


        
2条回答
  •  伪装坚强ぢ
    2021-02-07 22:09

    Could also split in to two like this

    async fetchData() {
            let config = {
              headers: {
                'Accept': 'application/json'  //or text/json
              }
            }
            fetch(http://localhost:3000/add`, config)
              .then(res => {
                return res.json();
              }).then(this.setResults);
    
              //setResults
              setResults(results) {
              this.details = results;
    
              //or: let details = results
    
              console.log(details)  (or: this.details)
    

提交回复
热议问题