简体   繁体   中英

Browser shows get request is made but nothing is returned in promise?

Currently stumped on an issue and not finding anything online to help me out. I am making a very basic HTTP get request to get a JSON object from an API I made (express+CORS enabled).

I've tried with both Axios and VueResource but having the same issue where my browser shows that the request is made and is successful (even shows the expected data).

表明http GET请求是OK的

But I never get any returns in the promise. And using both console.logs and breakpoints it shows the.then and.catch functions are never run.

  methods: {
    getTasks() {
      return this.$http.get("http://localhost:3080/api/tasks").then(response => function() {
        console.log("in"); // This console.log is never run
        this.data = response.data; // this.data is still null
      }).catch(err => {
        // No errors are returned
        console.log(err);
      });
    }
  },
  mounted() {
    this.getTasks();
  }

The correct syntax for arrow functions is:

 (params) => {
  // code
 }

Change your then callback to:

 getTasks() {
      return this.$http.get("http://localhost:3080/api/tasks").then(response => {
        console.log("in"); // This console.log is never run
        this.data = response.data; // this.data is still null
      }).catch(err => {
        // No errors are returned
        console.log(err);
      });
    } 

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