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

相关推荐
Dxy12393102162 天前
Ajax如何发送列表数据
前端·ajax·okhttp
96773 天前
AJAX和Axios理解和关系
前端·ajax·okhttp
必胜刻5 天前
AJAX 请求理解
前端·ajax·okhttp·前后端交互
android_cai_niao5 天前
OkHttp 使用教程:从入门到精通(Kotlin)
okhttp·kotlin
qwert103711 天前
跨域问题解释及前后端解决方案(SpringBoot)
spring boot·后端·okhttp
符哥200813 天前
基于 OkHttp+Retrofit 实现 JSON / 表单 / XML/Protobuf 数据格式全解析
okhttp·json·retrofit
XiaoLeisj15 天前
Android 网络编程入门到实战:HttpURLConnection、JSON 处理、OkHttp 与 Retrofit2
android·网络·okhttp·json·gson·retrofit2·jsonobjecy
凯鑫BOSS15 天前
Pbootcms查看详细报错信息
okhttp
vistaup2 个月前
OKHTTP 默认构建包含 android 4.4 的TLS 1.2 以及设备时间不对兼容
android·okhttp