当前位置:

POST 请求,Ajax 与 cookie

访客 2024-04-24 483 0

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

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~