【问题标题】:Browser shows get request is made but nothing is returned in promise?浏览器显示获取请求,但承诺没有返回任何内容?
【发布时间】:2021-02-04 05:42:00
【问题描述】:

目前在一个问题上遇到困难,并没有在网上找到任何可以帮助我的东西。我正在发出一个非常基本的 HTTP 获取请求,以从我制作的 API 中获取 JSON 对象(启用 express+CORS)。

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

但我从来没有在承诺中得到任何回报。同时使用 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();
  }

【问题讨论】:

    标签: javascript vue.js http promise get


    【解决方案1】:

    箭头函数的正确语法是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 2021-08-28
      • 1970-01-01
      • 2013-10-07
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多