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

相关推荐
nee~2 天前
Charles抓包
okhttp
赵得C13 天前
AJAX拦截器失效排查指南:当你的beforeSend有效但error/complete沉默时
前端·ajax·okhttp
粤M温同学17 天前
Android OkHttp 框架超时设置详解
android·okhttp
粤M温同学17 天前
Android 使用OkHttp 下载文件失败问题定位和修复
okhttp
CUIYD_198918 天前
Ajax 核心知识点全面总结
前端·ajax·okhttp
weixin_4383354018 天前
Spring RestTemplate + MultiValueMap vs OkHttp 多值参数的处理
java·spring·okhttp
网络点点滴21 天前
上传一个菜谱-最后部分(项目完结)
android·okhttp
农业工作者21 天前
Android:使用OkHttp
android·okhttp
androidwork21 天前
Android 中 OkHttp 的自定义 Interceptor 实现统一请求头添加
android·java·okhttp·kotlin
一只会跑会跳会发疯的猴子22 天前
ajax访问阿里云天气接口,获取7天天气
前端·ajax·okhttp