POST 请求,Ajax 与 cookie

POST 请求则需要设置RequestHeader告诉后台传递内容的编码方式以及在 send 方法里传入对应的值

xhr.open("POST", url, true);

xhr.setRequestHeader(("Content-Type": "application/x-www-form-urlencoded"));

xhr.send("key1=value1&key2=value2");

Ajax 与 cookie

ajax 会自动带上同源的 cookie,不会带上不同源的 cookie

可以通过前端设置 withCredentials 为 true, 后端设置 Header 的方式让 ajax 自动带上不同源的 cookie,但是这个属性对同源请求没有任何影响。会被自动忽略。

withCredentials | MDN

var xhr = new XMLHttpRequest();

xhr.open("GET", "http://example.com/", true);

xhr.withCredentials = true;

xhr.send(null);

相关推荐
前端百草阁5 小时前
【吃透 Promise】从基础到面试高频(手写 + 输出题 + 原理)
okhttp·面试·职场和发展
ppandss11 天前
JavaWeb从0到1-DAY4-AJAX
前端·ajax·okhttp
4311媒体网5 天前
织梦CMS点击率统计实现方法
okhttp
帅次6 天前
链路到端上:HTTPS 之后安全题还在考什么
android·okhttp·glide·zygote·retrofit
djk88887 天前
layui zTree 控件 AJAX绑定 点击tree事件 获取tree值
ajax·okhttp·layui
明天就是Friday9 天前
Android实战项目④ OkHttp WebSocket开发即时通讯App 完整源码详解
android·websocket·okhttp
xiangxiongfly9159 天前
Android 使用WebSocket通信
android·websocket·网络协议·okhttp
研☆香10 天前
聊聊什么是AJAX
前端·ajax·okhttp
sunwenjian88612 天前
跨域问题解释及前后端解决方案(SpringBoot)
spring boot·后端·okhttp
XiaoLeisj14 天前
Android 短视频播放详情页实战:从播放器模块拆分、Media3 与 FlowHelper 接入,到 ViewPager 高度适配和详情数据联动
android·okhttp·音视频·架构设计·flowhelper