我有一个ASP.Net应用程序,可以在domain1.mywebsite.com上创建一个cookie。
Private Sub CreateCookie()
If Request.Cookies("D1_MyWebSite") Is Nothing Then
Dim aCookie As New HttpCookie("D1_MyWebSite")
aCookie.Path = "/"
aCookie.Value = DateTime.Now.ToUniversalTime.ToString
aCookie.Expires = DateTime.Now.AddMinutes(10)
Response.Cookies.Add(aCookie)
Else
Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("D1_MyWebSite")
cookie.Value = DateTime.Now.ToUniversalTime.ToString
cookie.Expires = DateTime.Now.AddMinutes(10)
Response.Cookies.Add(cookie)
End If
End Sub
在domain2.mywebsite.com上,我试图使用jQuery读取cookie中保存的值。 网页地址是domain2.mywebsite.com/index.html,但此页面的.js文件来源是:
<script type="text/javascript" src="https://domain1.mywebsite.com/js/jumppage.js"></script>
<script src="https://domain1.mywebsite.com/js/jquery.cookie.js"></script>
我尝试使用以下内容获取domain1.mywebsite.com D1_MyWebSite cookie的值,但它所做的就是为domain2.mywebsite.com创建一个名为D1_MyWebSite的新cookie。如何获取domain1.mywebsite.com D1_MyWebSiteCookie的cookie值?
$.cookie("D1_MyWebSite", "value");
答案 0 :(得分:0)
终于弄明白了这个问题。我不得不删除.Path并添加.Domain。
Dim aCookie As New HttpCookie("D1_MyWebSite")
aCookie.Domain = ".mywebsite.com"
aCookie.Value = DateTime.Now.ToUniversalTime.ToString
我可以使用以下代码通过jQuery获取存储在domain2上的cookie中的datetime值:
var MyValue = $.cookie("D1_MyWebSite");
答案 1 :(得分:-1)
您必须将路径设置为“/”,如
aCookie.Path = "/";