博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue 项目中遇到的跨域问题及解决方法
阅读量:4538 次
发布时间:2019-06-08

本文共 2676 字,大约阅读时间需要 8 分钟。

原文:

问题描述

前端 vue 框架,跨域问题后台加这段代码

header("Access-Control-Allow-Origin: *");
加了之后报这个错:

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

解决办法

文章链接:CORS: credentials mode is ‘include'

xhrFields: { withCredentials: false},

把 withCredentials: true 改成 withCredentials: false,如果你没加上面那段代码当然也不会报这个错。虽然是解决方法很简单,但经此发现许多知识没掌握不得不梳理下。

HTTP 请求方式有许多种,有些请求会触发 CORS 预检请求。“需预检的请求”会使用 OPTIONS 方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。
对于跨域请求浏览器一般不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的 withCredentials 属性为 true:withCredentials: true。此时要求服务器的响应信息中携带 Access-Control-Allow-Credentials: true,否则响应内容将不会返回。

跨域请求想要带上 cookies 必须在请求头里面加上:

crossDomain: true, xhrFields: {  withCredentials: true}

又变成文章开头的问题了,解决办法:

后台代码:

Access-Control-Allow-Origin: 'http://www.zrt.local:8080'Access-Control-Allow-Credentials: true

前端代码:

crossDomain: true, xhrFields: {  withCredentials: true}

跟之前一样就行了。

总结

1.不限定跨域地址,不携带身份凭证

.NET CORE

services.AddCors(                options => options.AddPolicy(                    _defaultCorsPolicyName,                    builder => builder                         .AllowAnyOrigin()                        .AllowAnyHeader()                        .AllowAnyMethod()                        .AllowCredentials()                )            );

PHP后台代码:

header("Access-Control-Allow-Origin: *");

前端代码:

xhrFields: { withCredentials: false},

2.指定授权访问地址,携带身份凭证(带cookies)

.NET CORE

services.AddCors(                options => options.AddPolicy(                    _defaultCorsPolicyName,                    builder => builder                        .WithOrigins(                            // App:CorsOrigins 在 appsettings.json 中,可配多个地址                            _appConfiguration["App:CorsOrigins"]                               .Split(",", StringSplitOptions.RemoveEmptyEntries)                                .Select(o => o.RemovePostFix("/"))                                .ToArray()                        )                         .AllowAnyHeader()                        .AllowAnyMethod()                        .AllowCredentials()                )            );

PHP后台代码:

Access-Control-Allow-Origin: 'http://www.zrt.local:8080'Access-Control-Allow-Credentials: true

前端代码:

crossDomain: true, xhrFields: {  withCredentials: true}

转载于:https://www.cnblogs.com/xcsn/p/11272959.html

你可能感兴趣的文章
Oracle -操作数据库
查看>>
c - 给分数分级别
查看>>
chrome 调试
查看>>
luoguP2774 方格取数问题
查看>>
tcp/ip协议各层的理解与
查看>>
python中的setdefault()方法
查看>>
转 VSFTP用户权限管控
查看>>
poj2420 A Star not a Tree? 模拟退火
查看>>
微信小程序--登录授权,一键获取用户微信手机号并登录
查看>>
[转载] C#面向对象设计模式纵横谈——13. Proxy代理模式
查看>>
JqueryEasyUI浅谈---视频教程公布
查看>>
ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致”...
查看>>
Javaweb之 servlet 开发详解1
查看>>
Restore IP Addresses
查看>>
DWR框架简单应用
查看>>
KMP 学习心得-----转
查看>>
time.strftime:格式化字符串中含中文报错处理
查看>>
模态窗口缓存无法清除怎么办? 在地址上加个随机数吧"&rd=" + new Date().getTime()
查看>>
阿里的weex框架到底是什么
查看>>
Tesis enDYNA
查看>>