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

相关推荐
太阳伞下的阿呆1 天前
HttpClient、OkHttp 和 WebClient
okhttp·httpclient·webclient
2503_928411561 天前
9.8 ajax+php基础语法
ajax·okhttp·php
小妖怪的夏天2 天前
react native 出现 FATAL EXCEPTION: OkHttp Dispatcher
react native·react.js·okhttp
阿华的代码王国9 天前
【Android】OkHttp发起GET请求 && POST请求
android·java·okhttp·网络连接
ahoges10 天前
easy-http类似feign的轻量级http客户端工具
java·okhttp
java_t_t13 天前
HTTP 接口调用工具类(OkHttp 版)
网络协议·http·okhttp
雨白19 天前
OkHttpClient 核心配置详解
android·okhttp
小白学大数据25 天前
1688商品数据抓取:Python爬虫+动态页面解析
爬虫·python·okhttp
minos.cpp1 个月前
第一章 OkHttp 是怎么发出一个请求的?——整体流程概览
android·okhttp·面试