【问题标题】:VueJs Axios Access-Control-Allow-Origin CORS errorVueJs Axios Access-Control-Allow-Origin CORS 错误
【发布时间】:2021-06-17 20:15:34
【问题描述】:

我用 Vue3 和 Laravel 8 创建了一个网站。我尝试使用 axios 将数据从 vue 发布到 laravel,但我总是遇到 CORS 错误。我尝试了一切,但没有任何效果。 You can check the error here

向 axios 添加标头

addUser() {
      axios
        .post("http://localhost:8000/api/user/store", this.user, {
          headers: { "Access-Control-Allow-Origin": "*" }
        })
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
        })
        .finally(() => (this.loading = false));
    },

向 package.json 添加代理

"proxy": "http://127.0.0.1:8000",

并尝试过: https://enable-cors.org/server_apache.html

我能做什么?

【问题讨论】:

  • 我不知道,但部分问题可能是同时使用 localhost127.0.0.1,尝试使用其中一个,它可能会解决 CORS 问题。例如,将您的 axios.post 更改为 http://127.0.0.1/api/user/store 是否有效?
  • 是的,它的工作。谢谢! :)

标签: laravel vue.js axios cors cors-anywhere


【解决方案1】:

尝试添加

axios.defaults.withCredentials = true;

到 bootstrap.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2015-08-17
    • 2014-08-11
    • 2021-05-12
    • 2018-12-10
    • 2021-12-02
    • 1970-01-01
    • 2021-03-07
    相关资源
    最近更新 更多