如何将cookie附加到我的jQuery post请求标头?

时间:2022-03-10 07:04:40

Related to How can I replicate this cookie functionality on my Web Client?

与如何在Web客户端上复制此cookie功能相关?

I discovered in a previous question that I need to set a cookie in my request header.

我在上一个问题中发现我需要在请求标头中设置一个cookie。

I call a login method on a 3rd party web service, this returns a cookie that I need to use on subsequent requests to that server. I had assumed that the browser keeps this cookie and attaches it to the appropriate request, however, this does not seem to be the case (I can see it isn't in firebug).

我在第三方Web服务上调用登录方法,这将返回我需要在对该服务器的后续请求中使用的cookie。我曾假设浏览器会保留此cookie并将其附加到相应的请求中,但是,这似乎并非如此(我可以看到它不在firebug中)。

How can I set the cookie on my request header?

如何在我的请求标题上设置cookie?

My call is as follows:

我的电话如下:

var call = serviceUriString + '?' + strRequest;
$.post(call, function(d) {}, 'text').success(function(d) { ..//do something

I think I may have to use jQuery.ajax( url [, settings] ) beforesend, but I can't find any good examples of how to set the cookie, or indeed how to get it out / persist it from the response from the previous login request (so that I can then attach it).

我想我可能不得不在使用之前使用jQuery.ajax(url [,settings]),但是我找不到任何关于如何设置cookie的好例子,或者实际上如何从响应中找到它/坚持它先前的登录请求(以便我可以附加它)。

As @edr points out, I am doing this client side. So my website calls a web service in client side code.

正如@edr指出的那样,我正在做这个客户端。所以我的网站在客户端代码中调用Web服务。

2 个解决方案

#1


1  

I'm not sure if there is an answer to this, however I can add ;jsessionid=[value_here]? to my url and that works.

我不确定是否有答案,但是我可以添加; jsessionid = [value_here]?到我的网址,这是有效的。

For example:

var call = serviceUriString + ';jsessionid=' + [value_here] + '?' + strRequest;
$.post(call, function(d) {}, 'text').success(function(d) { ..//do something

similar answer is here: https://*.com/a/10353875

类似的答案在这里:https://*.com/a/10353875

#2


0  

Just speculating here:

只是在这里推测:

Since cookies go along for the ride when doing ajax calls I suspect that the cookie created is for the 3rd party domain so it stand to reason that you will not find it in the cookies for your domain.

由于在执行ajax调用时cookie会随身携带,因此我怀疑创建的cookie是针对第三方域的,因此您可能无法在域中的cookie中找到它。

Try adding the cookie in client code before doing the post. This SO question may help:

尝试在发布帖子之前在客户端代码中添加cookie。这个问题可能会有所帮助:

How do I set/unset cookie with jQuery?

如何使用jQuery设置/取消设置cookie?

#1


1  

I'm not sure if there is an answer to this, however I can add ;jsessionid=[value_here]? to my url and that works.

我不确定是否有答案,但是我可以添加; jsessionid = [value_here]?到我的网址,这是有效的。

For example:

var call = serviceUriString + ';jsessionid=' + [value_here] + '?' + strRequest;
$.post(call, function(d) {}, 'text').success(function(d) { ..//do something

similar answer is here: https://*.com/a/10353875

类似的答案在这里:https://*.com/a/10353875

#2


0  

Just speculating here:

只是在这里推测:

Since cookies go along for the ride when doing ajax calls I suspect that the cookie created is for the 3rd party domain so it stand to reason that you will not find it in the cookies for your domain.

由于在执行ajax调用时cookie会随身携带,因此我怀疑创建的cookie是针对第三方域的,因此您可能无法在域中的cookie中找到它。

Try adding the cookie in client code before doing the post. This SO question may help:

尝试在发布帖子之前在客户端代码中添加cookie。这个问题可能会有所帮助:

How do I set/unset cookie with jQuery?

如何使用jQuery设置/取消设置cookie?