简体   繁体   English

首次请求时缺少Javascript制作的Cookie

[英]Javascript-made cookie missing on first Request

I have made a shopping cart that creates a cart, adds products and saves it in a cookie called 'cart'. 我制作了一个购物车,可以创建购物车,添加产品并将其保存在名为“购物车”的Cookie中。

When I click a hyperlink that sends me to my checkout page, my ASP.NET application doesn't have access to the cookie. 当我单击将我发送到我的结帐页面的超链接时,我的ASP.NET应用程序无权访问Cookie。 BUT when I refresh the page, the cookie gets sent with the request. 但是,当我刷新页面时,cookie随请求一起发送。

Is there any way I can get a cookie to be sent with the request on the first request?? 有什么办法可以使Cookie与第一个请求一起发送?

Thanks! 谢谢!

Edit: 编辑:

My lifecycle would be: 我的生命周期将是:

add cart with product to cookie using javascript -> user clicks basket -> we show the basket 使用JavaScript将包含产品的购物车添加到Cookie->用户点击购物篮->我们显示购物篮

When the user clicks the basket initially, the request to the server doesn't contain the cookie. 当用户最初单击购物篮时,对服务器的请求不包含cookie。 It is only when the user refreshes that I see the updated cart on the server. 只有在用户刷新时,我才能在服务器上看到更新的购物车。

Any idea? 任何想法?

I don't believe this is possible. 我不认为这是可能的。 The HTTP lifecycle is, client request -> HTML / javascript -> client Post. HTTP生命周期是客户端请求-> HTML / javascript->客户端发布。 Since the javascript doesn't get run until after the first request, I don't see how it would be possible to get a javascript created cookie on the first go round. 由于javascript直到第一个请求之后才开始运行,所以我看不到如何在第一轮获取javascript创建的cookie。

Within your .Net controller you should be able to call any cookie. 在您的.Net控制器中,您应该能够调用任何cookie。

Using MVC 3 here is how I have it being done: 这是我使用MVC 3的方式:

using System.Web;

HttpCookie cookieObjName = Request.Cookies.Get("yourCookieNameHere");

//Then you can get the value with:
cookieObjName.Value;

You should do some sort of verification of data to make sure the cookie hasn't been tampered with. 您应该对数据进行某种形式的验证,以确保Cookie未被篡改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM