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 天前
1688商品数据抓取:Python爬虫+动态页面解析
爬虫·python·okhttp
minos.cpp3 天前
第一章 OkHttp 是怎么发出一个请求的?——整体流程概览
android·okhttp·面试
Sy_planA6 天前
介绍一下jQuery的AJAX异步请求
ajax·okhttp·jquery
2401_8370885012 天前
AJAX简介
okhttp
2401_8370885014 天前
Axios介绍
android·okhttp
魑魅魍魉都是鬼19 天前
白玩 一 记录retrofit+okhttp+flow 及 kts的全局配置
okhttp·retrofit
小白学大数据21 天前
Python + Requests库爬取动态Ajax分页数据
开发语言·python·ajax·okhttp
小毛驴8501 个月前
JavaScript AJAX 实现,演示如何将 Token 添加到 Authorization
javascript·ajax·okhttp
安卓开发者1 个月前
OkHttp 与 Room 结合使用:构建高效的 Android 本地缓存策略
android·okhttp·缓存
安卓开发者1 个月前
OkHttp 与 Chuck 结合使用:优雅的 Android 网络请求调试方案
android·okhttp