简体   繁体   中英

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'.

When I click a hyperlink that sends me to my checkout page, my ASP.NET application doesn't have access to the cookie. BUT when I refresh the page, the cookie gets sent with the request.

Is there any way I can get a cookie to be sent with the request on the first request??

Thanks!

Edit:

My lifecycle would be:

add cart with product to cookie using javascript -> user clicks basket -> we show the basket

When the user clicks the basket initially, the request to the server doesn't contain the 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. 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.

Within your .Net controller you should be able to call any cookie.

Using MVC 3 here is how I have it being done:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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