在 javascript 中的 HTTP POST 请求中发送 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35733138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:09:36  来源:igfitidea点击:

Send cookie in HTTP POST Request in javascript

javascriptcookieshttp-headershttp-posthttprequest

提问by arpit joshi

I am trying to make a POST request to the server (Which is a REST service)via javascript,and in my request i want to send a cookie.My below code is not working ,as I am not able to receive cookie at the server side.Below are my client side and server side code.

我正在尝试通过 javascript 向服务器(这是一个 REST 服务)发出 POST 请求,并且在我的请求中我想发送一个 cookie。我下面的代码不起作用,因为我无法在服务器上接收 cookie side.Below 是我的客户端和服务器端代码。

Client side :

客户端 :

var client = new XMLHttpRequest();
          var request_data=JSON.stringify(data);
var endPoint="http://localhost:8080/pcap";
var cookie="session=abc";
          client.open("POST", endPoint, false);//This Post will become put 
          client.setRequestHeader("Accept", "application/json");
          client.setRequestHeader("Content-Type","application/json");

          client.setRequestHeader("Set-Cookie","session=abc");
          client.setRequestHeader("Cookie",cookie);
          client.send(request_data);

Server Side:

服务器端:

public @ResponseBody ResponseEntity getPcap(HttpServletRequest request,@RequestBody PcapParameters pcap_params ){

Cookie cookies[]=request.getCookies();//Its coming as NULL
        String cook=request.getHeader("Cookie");//Its coming as NULL
}

回答by Quentin

See the documentation:

请参阅文档

Terminate these steps if header is a case-insensitive match for one of the following headers … Cookie

如果标头与以下标头之一不区分大小写,则终止这些步骤…… Cookie

You cannot explicitly set a Cookie header using XHR.

您不能使用 XHR 显式设置 Cookie 标头。



It looks like you are making a cross origin request (you are using an absolute URI).

看起来您正在发出跨源请求(您使用的是绝对 URI)。

You can set withCredentialsto include cookies.

您可以设置withCredentials为包含 cookie。

True when user credentials are to be included in a cross-origin request. False when they are to be excluded in a cross-origin request and when cookies are to be ignored in its response. Initially false.

当用户凭据包含在跨域请求中时为真。当它们要在跨域请求中被排除并且在其响应中要忽略 cookie 时为 False。最初是假的。

Such:

这样的:

client.withCredentials = true;

This will only work if http://localhost:8080has set a cookie using one of the supported methods (such as in an HTTP Set-Cookieresponseheader).

这仅在http://localhost:8080使用支持的方法之一(例如在 HTTPSet-Cookie响应标头中)设置 cookie时才有效。



Failing that, you will have to encode the data you wanted to put in the cookie somewhere else.

否则,您将不得不对要放入 cookie 中的数据进行编码。

回答by PDStat

This can also be done with the more modern fetch

这也可以通过更现代的 fetch

fetch(url, {
    method: 'POST',
    credentials: 'include'
    //other options
}).then(response => console.log("Response status: ", response.status));