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 小时前
【JavaScript】通过AJAX技术让前端发请求到后端
javascript·ajax·okhttp
XiYang-DING19 小时前
【Spring】 Ajax
spring·ajax·okhttp
hj10432 天前
fastadmin 开发示例:使用 layer 弹窗实现数据快速录入
okhttp
YF02116 天前
Android App 高效升级指南:OkDownload 多线程断点续传与全版本安装适配
android·okhttp·app
YF02118 天前
深度解构Android OkDownload断点续传
android·数据库·okhttp
右耳朵猫AI11 天前
Python技术周刊 2026年第14周
开发语言·python·okhttp
牢七11 天前
cve研究
okhttp
yqcoder12 天前
原生 AJAX 揭秘:如何使用 XHR 发起请求
前端·ajax·okhttp
身如柳絮随风扬14 天前
你知道什么是 Ajax 吗?—— 从入门到原理,一篇彻底搞懂
前端·ajax·okhttp
小白学大数据19 天前
Python 自动化爬取网易云音乐歌手歌词实战教程
爬虫·python·okhttp·自动化