这个跨域问题比较奇怪

16.html:1 Access to XMLHttpRequest at img.example.com/parse?abb=xxx&... (redirected from img.example.com/parse?abb=xxx&p...) from origin ‘http://x.cese.top' has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

这是浏览器中控制台弹出来的错误,
客户会在他的页面中引入我们提供的一个JS 这个JS会向我们服务器批量发送一些请求
但是这些请求有时候 全部成功 有时候 全部失败 或者部分成功或者失败, 这就让人摸不着头脑了
看这个报错的信息是 Access-Control-Allow-Origin 不允许他跨域 我在我们laravel 的系统中 做了一个全局中间件 Access-Control-Allow-Origin 这一项设置的是* ,并且在nginx中也设置了一个 Access-Control-Allow-Origin *

客户有时候能成功也证明了这个设置是成功的,
但是他有时候还是会失败 依然是这个跨域错误 我就搞不懂了
有人遇上过吗

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3
porygonCN

有时候报错就会跨域 抛出错误之后走错误处理就不会走允许跨域中间件了

3年前 评论
Lsw_7777777 3年前
Adachi 3年前
porygonCN (作者) 3年前
Adachi 3年前

使用laravel-cors依赖并且在app\Http\Middleware\VerifyCsrfToken文件中修改如下:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '*'
    ];
}
3年前 评论
随波逐流

跨域如果使用nginx解决, 在Laravel中就不要添加, 防止http 响应头冲突

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!