浏览器显示已发出获取请求但未承诺返回任何内容?

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

目前遇到了一个问题,在网上找不到任何可以帮助我解决的问题。我正在发出一个非常基本的 HTTP get 请求,以从我创建的 API 中获取一个 JSON 对象(启用 express+CORS)。

我尝试过使用 Axios 和 VueResource,但遇到同样的问题,我的浏览器显示请求已发出并成功(甚至显示预期的数据)。

但我从来没有得到任何 returns 的承诺。同时使用 console.logs 和断点显示 .then 和 .catch 函数永远不会 运行.

  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();
  }

箭头函数的正确语法是:

 (params) => {
  // code
 }

将您的 then 回调更改为:

 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);
      });
    }