无法使用jquery更改cookie值

时间:2014-01-20 10:51:12

标签: jquery jquery-cookie

我在网上找不到答案,所以这里有:

我正在从查询字符串中读取一个字符串,并使用jquery cookie插件将其保存在cookie中。

此页面可能每天使用不同的ID重新加载几次,但似乎始终记住第一个值。似乎很难让cookie每次都更改为新值。

$.cookie('SomeId')  // already contains some number from the first time the page was called.

我试图使用的逻辑是:

if (QueryString.some_id) {
    alert(QueryString.some_id); //shows new Id
    $.cookie('SomeId', null, { expires: -1 }); //Tried deleting old cookie first
    $.cookie('SomeId', QueryString.some_id, { expires: 30, path: '/' }); // Also tried without the path param
    alert($.cookie('SomeId')); // Alerts the OLD id. Previous call didn't work.
}

如何让$.cookie('SomeId')可以更新?

如果它很重要,我怀疑它可能,我通过使用IIS在本地运行项目来运行这些测试。这是一个MVC应用程序。

谢谢

1 个答案:

答案 0 :(得分:0)

我有一段时间没有这个问题。我通过组建一个测试域来修复它[我使用clientname.loc并为每个项目创建一个新的]并将其放在您的主机文件[C:\ Windows \ System32 \ Drivers \ etc \ hosts]中。

#existing hosts file entries
127.0.0.1 clientname.loc

或者你可以使用127.0.0.1代替localhost,因为它显然适用于。