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

相关推荐
咖啡の猫2 天前
Vue 实例生命周期
前端·vue.js·okhttp
Jeled5 天前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
Jeled6 天前
Android 网络层最佳实践:Retrofit + OkHttp 封装与实战
android·okhttp·kotlin·android studio·retrofit
allk558 天前
OkHttp源码解析(一)
android·okhttp
allk558 天前
OkHttp源码解析(二)
android·okhttp
aFakeProgramer8 天前
拆分PDF.html 办公小工具
okhttp
一壶浊酒..10 天前
ajax局部更新
前端·ajax·okhttp
洛克大航海13 天前
Ajax基本使用
java·javascript·ajax·okhttp
whltaoin19 天前
Java 网络请求 Jar 包选型指南:从基础到实战
java·http·okhttp·网络请求·retrofit
华农第一蒟蒻20 天前
谈谈跨域问题
java·后端·nginx·安全·okhttp·c5全栈