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);

相关推荐
你觉得脆皮鸡好吃吗18 小时前
XSS渗透 COOKIE
网络·http·okhttp·网络安全学习
彭于晏Yan4 天前
OkHttp 与 RestTemplate 技术选型对比
java·spring boot·后端·okhttp
JohnnyDeng944 天前
OkHttp 拦截器链与缓存策略:深度解析网络层的核心机制
okhttp·缓存
胖胖胖胖胖虎4 天前
okhttp Stream Load 含认证请求重定向
starrocks·okhttp
步十人5 天前
【JavaScript】通过AJAX技术让前端发请求到后端
javascript·ajax·okhttp
XiYang-DING5 天前
【Spring】 Ajax
spring·ajax·okhttp
hj10436 天前
fastadmin 开发示例:使用 layer 弹窗实现数据快速录入
okhttp
YF021110 天前
Android App 高效升级指南:OkDownload 多线程断点续传与全版本安装适配
android·okhttp·app
YF021112 天前
深度解构Android OkDownload断点续传
android·数据库·okhttp
右耳朵猫AI15 天前
Python技术周刊 2026年第14周
开发语言·python·okhttp