【问题标题】:The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin error“Access-Control-Allow-Origin”标头的值不等于提供的来源错误
【发布时间】:2020-09-30 20:46:40
【问题描述】:

我得到这个错误:

从源“http://localhost:8080”访问“http://127.0.0.1:8000/api/user”处的 XMLHttpRequest 已被 CORS 策略阻止:“Access-Control-Allow-Origin”标头的值“http://localhost:8080/*”不相等到提供的原点。

我已经创建了中间件,没有任何改变:

public function handle($request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin','http://localhost:8080/*')
        ->header('Access-Control-Allow-Methods','POST,PUT,GET,DELETE,OPTIONS')
        ->header('Access-Control-Allow-Headers','Accept,Authorization,Content-Type')
        ->header('Content-Type','application/json');
}

【问题讨论】:

    标签: laravel vue.js axios


    【解决方案1】:

    我不确定但也许 chorme 不支持localhost

    尝试使用通配符* 而不是http://localhost:8080/*

    【讨论】:

    • 我更改了 * 而不是 localhost:8080* 并收到此错误:从源 'blog.test/api/user' 访问 XMLHttpRequest 已被 CORS 策略阻止:'的值当请求的凭据模式为“包含”时,响应中的 Access-Control-Allow-Origin 标头不得为通配符“*”。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
    • 你的允许凭据是 true 现在解决这个问题 false 你在前端允许凭据
    • 因为如果它托管在域上,您将在 localhost 上对其进行测试,您将不会收到此错误...每当您使用 * 关闭您的 allowcredentials
    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 2016-10-28
    • 2017-12-16
    • 2020-11-12
    • 2013-12-29
    • 2017-11-30
    • 2017-09-20
    • 2015-09-28
    相关资源
    最近更新 更多