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

相关推荐
androidwork7 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
Lhuu(重开版12 小时前
Vue:Ajax
vue.js·ajax·okhttp
知月玄2 天前
网页前端开发(基础进阶4--axios)
okhttp
余渔鱼11232 天前
ajax学习手册
学习·ajax·okhttp
smallluan3 天前
入门AJAX——XMLHttpRequest(Post)
前端·ajax·okhttp
志存高远666 天前
(面试)OkHttp实现原理
okhttp
隐-梵7 天前
Android studio进阶开发(七)---做一个完整的登录系统(前后端连接)
android·数据库·ide·spring·okhttp·android studio
隐-梵13 天前
Android studio进阶开发(六)--如何用真机通过okhttp连接服务器
服务器·数据库·okhttp·android studio
每次的天空14 天前
Android-OkHttp与Retrofit学习总结
android·okhttp·retrofit
tmacfrank14 天前
Android 网络全栈攻略(四)—— 从 OkHttp 拦截器来看 HTTP 协议一
android·网络·okhttp