简体   繁体   English

浏览器显示获取请求,但在 promise 中没有返回任何内容?

[英]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).我正在制作一个非常基本的 HTTP 获取请求,以从启用 ZDB974238714CA38DE634CORA7ACED14 的 ZDB974238714CA8DE63ZCORA7C14

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).我已经尝试过 Axios 和 VueResource 但有同样的问题,我的浏览器显示请求已发出并且成功(甚至显示了预期的数据)。

表明http GET请求是OK的

But I never get any returns in the promise.但我从未在 promise 中获得任何回报。 And using both console.logs and breakpoints it shows the.then and.catch functions are never run.并同时使用 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();
  }

The correct syntax for arrow functions is:箭头函数的正确语法是:

 (params) => {
  // code
 }

Change your then callback to:将您的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);
      });
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM