has been blocked by CORS policy: The ‘Access-Control-Allow-Origin‘ header contains multiple values ‘

控制台错误如下:

Access to XMLHttpRequest at 'https://xxx/xxx' from origin 'https://xxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Access to XMLHttpRequest at 'https://xxx/xx' from origin 'https://xxx' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.

原因:项目中设置了双跨域配置导致的问题

解决方法:

删除或注解掉一处,只保留一个。

注释掉nginx上的相关配置

    location / {
		root /var/www/html/xxx/dist/;
		index index.html;

		# 配置响应请求
		try_files $uri $uri/ /index.html$is_args$args;
	}
	location /api/ {
		proxy_pass http://localhost:xx/;
		add_header 'Access-Control-Allow-Origin' '*';
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
		add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Max-Age' 1728000;
			add_header 'Content-Type' 'text/plain; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}

后端代码不动

之后又出现 CORS 头缺少 'Access-Control-Allow-Origin' 跨域问题

已拦截跨源请求:同源策略禁止读取位于 https://xxx 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。状态码:204。

已拦截跨源请求:同源策略禁止读取位于 https://xxx 的远程资源。(原因:CORS 请求未能成功)。状态码:(null)。

发现还是报错,对nginx配置文件进行如下修改:

    location /api/ {
		proxy_pass http://localhost:xx/;
		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Max-Age' 1728000;
			add_header 'Content-Type' 'text/plain; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}
	}
	location / {
		add_header 'Access-Control-Allow-Origin' '*';
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
		add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
		root /var/www/html/xxx/dist/;
		index index.html;

		# 配置响应请求
		try_files $uri $uri/ /index.html$is_args$args;
	}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
此插件为跨域插件,打开后可跨域访问接口,旧版浏览器可直接拖到扩展程序中安装,新版浏览器需要将扩展名修改成rar并解压,然后点击“加载已解压的扩展程序”安装。 ======================插件概述谷歌译文====================== 轻松将(Access-Control-Allow-Origin:*)规则添加到响应标头。 允许CORS:通过Access-Control-Allow-Origin,您可以轻松地在Web应用程序中执行跨域Ajax请求。 只需激活插件并执行请求。默认情况下(在JavaScript API中),CORS或跨源资源共享在现代浏览器中被阻止。安装此加载项将使您可以解除阻止此功能。请注意,将插件添加到浏览器后,默认情况下它处于非活动状态(工具栏图标为灰色C字母)。如果要激活加载项,请按一次工具栏图标。图标将变为橙色的C字母。 ======================插件概述谷歌译文====================== ========================插件概述原文======================== Easily add (Access-Control-Allow-Origin: *) rule to the response header. Allow CORS: Access-Control-Allow-Origin lets you easily perform cross-domain Ajax requests in web applications. Simply activate the add-on and perform the request. CORS or Cross Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs). Installing this add-on will allow you to unblock this feature. Please note that, when the add-on is added to your browser, it is in-active by default (toolbar icon is grey C letter). If you want to activate the add-on, please press on the toolbar icon once. The icon will turn to orange C letter. ========================插件概述原文========================

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值