首页 » 网站推广 » phpsession偶然丧失技巧_vue前后端分离运用fetch 跨域请求时 session失落效问题解决

phpsession偶然丧失技巧_vue前后端分离运用fetch 跨域请求时 session失落效问题解决

访客 2024-12-08 0

扫一扫用手机浏览

文章目录 [+]

fd.append('username',this.userName);

fd.append('password',this.userPwd);

phpsession偶然丧失技巧_vue前后端分离运用fetch 跨域请求时 session失落效问题解决

fd.append('checkCode',this.code);

phpsession偶然丧失技巧_vue前后端分离运用fetch 跨域请求时 session失落效问题解决
(图片来自网络侵删)

fetch(`${apiUrl}/login`,{

method:'post',

body:fd,

credentials: 'include'

}).then((res)=>res.json()).

then((res)=>{

console.log(res);

}).catch((res)=>console.log(res));

后台代码

(注:

response.setHeader(\公众Access-Control-Allow-Origin\公众, \公众http://192.168.1.136:8080\"大众;);

response.setHeader(\公众Access-Control-Allow-Methods\公众, \"大众POST, GET, OPTIONS, DELETE\公众);

response.setHeader(\"大众Access-Control-Max-Age\"大众, \公众3600\"大众);

response.setHeader(\"大众Access-Control-Allow-Headers\"大众, \"大众Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token\公众);

response.setHeader(\"大众Access-Control-Allow-Credentials\公众, \公众true\公众);//是否支持cookie跨域

后台什么都不须要加,根本不须要加跨域这些东西)。

javascript 利用fetch进行跨域要求时默认是不带cookie的,以是会造成 session失落效。

fetch(url, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: JSON.stringify({ data: options.data }) })credentials: 'include' 可以是fetch 带上cookie。
但是问题了来。

原来在做事器端设置header (php 做事器)

header(\公众Access-Control-Allow-Origin: \"大众);

会报错:

A wildcard '' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8000' is therefore not allowed access.

可以看到不许可 利用‘’ 号了,那么就改成 访问域名(这里是本地调用所以是 http://localhost:8000)

header(\"大众Access-Control-Allow-Origin: http://localhost:8000\公众);

改完后再次发送要求,还是报错

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http://localhost:8000' is therefore not allowed access.

说'Access-Control-Allow-Credentials 头必须是true,那么连续增加

header(\"大众Access-Control-Allow-Credentials: true\公众);

增加完后可以正常访问了,而且session也有了。

ps: fetch 有个mode 是no-cors ,创造设置后返回的status是0,查资料后

no-cors mode is only to CDN content, such as scripts, CSS and image, you cannot be used for getting data,response.status = 0 is right behavior

no-cors 模式只能用来获取CDN内容,比如脚本,css文件和图片,如果用来获取数据比如json格式就会返回status=0

针对付办理前后台sessionid同等问题往后又碰着 参数通报不到后台,末了这样办理:

终极方案(推举利用):

既然fetch不会自动转FormData,那我们自己new一个FormData,直接传给body。

在FormData中也可以通报字节流实现上传图片的功能。
参考:http://blog.csdn.net/codetomylaw/article/details/52446786

[javascript] view plain copy

let formData = new FormData();

formData.append(\"大众name\"大众,\"大众admin\公众);

formData.append(\"大众password\"大众,\"大众admin123\"大众);

etch(url , {

method: 'POST',

headers: {},

body: formData,

).then((response) => {

if (response.ok) {

return response.json();

}

).then((json) => {

alert(JSON.stringify(json));

).catch((error) => {

console.error(error);

);

这样我们就可以在Server端获取到name和password的值了。

标签:

相关文章

丰镇市,历史文化与现代发展的交融之地

丰镇市,位于我国内蒙古自治区乌兰察布市中部,是一座历史悠久的城市。自古以来,这里就是兵家必争之地,也是南北文化交流的重要通道。如今...

网站推广 2024-12-23 阅读0 评论0

串行通信协议,构筑现代通信的基石

随着信息技术的飞速发展,通信技术在各个领域都扮演着至关重要的角色。而在众多的通信方式中,串行通信因其高效、可靠的特点,成为了构筑现...

网站推广 2024-12-23 阅读0 评论0

主播推广协议,开启新媒体营销新时代

随着互联网的快速发展,新媒体营销已成为企业品牌推广的重要手段。其中,主播推广以其独特的优势,受到越来越多企业的青睐。在主播推广的过...

网站推广 2024-12-23 阅读0 评论0

主板代码F,介绍计算机心脏的神秘面纱

在科技飞速发展的今天,计算机已经成为了我们生活中不可或缺的一部分。而主板,作为计算机的核心组成部分,承载着整个系统的稳定运行。主板...

网站推广 2024-12-23 阅读0 评论0