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

相关推荐
mmsx18 小时前
基于 Android 的校园信息管理系统源码
android·java·okhttp
Full Stack Developme8 天前
跨站请求伪造 (CSRF) 是什么 设计及工作原理
前端·okhttp·csrf
weixin_4407841110 天前
【OkHttp实现原理】
android·java·okhttp
AZaLEan__22 天前
原生XMLHttpRequest + axios + fetch
okhttp
Android-Flutter22 天前
OkHttp 总结
android·okhttp·kotlin
zhangjin11201 个月前
Okhttp系列:POST请求
okhttp
এ慕ོ冬℘゜1 个月前
深入理解 JavaScript 事件体系:Window、鼠标与键盘事件详解
开发语言·javascript·okhttp
牢七1 个月前
wordpress
okhttp
脉动数据行情1 个月前
Java OkHttp 完整封装 GC COMEX 美黄金实时盘口定时拉取工具类
java·okhttp·comex 黄金·外盘贵金属