在vue中用axios获取数据,但dat

在vue中用axios获取数据,但data返回的是html内容

我调用的是网易云api

img

分— 割— 线——

<template>
  <div></div>
</template>

<script>
import axios from "axios";
import { onMounted, ref } from "vue";
export default {
  setup() {
    onMounted(() => {
      // 这里的路径不应该有#,但有#竟然不报错!我真的是懵了
      axios.get("http://localhost:8080/#/artist/sublist").then((res) => {
        console.log(res);
      });
    });
  },
};
</script>


这个路径我加了#,我知道不应该加,好离谱的是打印出的状态是200,没有报错。我真的懵了。

img

好,我把#删了,竟然报404错误了!我哭了,脑袋想破都想不出!

<template>
  <div></div>
</template>


<script>
import axios from "axios";
import { onMounted, ref } from "vue";
export default {
  setup() {
    onMounted(() => {
      axios.get("http://localhost:8080/artist/sublist").then((res) => {
        console.log(res);
      });
    });
  },
};
</script>

img

我用的是history模式

import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;


不要写全路径,写相对路径,配个网络代理就好了。这都不是啥问题,有没有#有啥关系,反正能请求到数据就行了

```html

http://localhost:8080/artist/sublist

``` 这个 http://localhost:8080 换成 接口前缀 ,应该有个统一得前缀吧。 如果涉及到跨域还需要设置代理

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^