【问题标题】:axios calls backend-api but theres no data displayedaxios 调用 backend-api 但没有数据显示
【发布时间】:2021-01-20 16:02:44
【问题描述】:

我想进行本地 API 调用,以从本地后端检索 base64 编码图像。 后端的日志显示 axios 调用了 api。 但是在我的前端没有显示数据。我得到的只是一个空页面。 我做错了什么? 谢谢我的任何帮助。

<template>
  <p>{{ this.result }}</p>
</template>

<script>
import axios from "axios";
export default {
  name: "App",
  components: {},
  data() {
    return {
      result: null,
    };
  },
  mounted() {
    axios
      .get("url")
      .then((response) => (this.result = response));
  },
};
</script>

【问题讨论】:

  • axios 中的数据在data 键下。将挂载函数中的 then() 调用更改为 .then((response) =&gt; (this.result = response.data));
  • url 包含什么?
  • .then(response =&gt; console.log(response)) 可能会提供一些有用的信息。
  • 感谢您的回复! @Anastazy 我有点错过了。我确实改变了它,但我仍然得到一个空白页面。 “url”是我的后端 API 的占位符,这部分确实有效,我的记录器会验证 API 是否被调用。

标签: javascript vue.js axios


【解决方案1】:

错误是“原因:CORS 标头 'Access-Control-Allow-Origin' 缺失”。

我必须为我的 Vue 应用程序启用跨域资源共享。这是在 Spring Boot 中使用 @CrossOrigin 完成的。

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2021-09-11
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2019-09-11
    • 2020-05-27
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多