简体   繁体   English

使用JQuery和Cookie插件创建一个非url编码值的cookie

[英]Create a cookie with a non url encoded value using JQuery and the Cookie plugin

I am creating a cookie on the client side and adding 3 string values in it separated by commas. 我在客户端创建一个cookie,并在其中添加3个字符串值,用逗号分隔。 The string have special characters. 该字符串具有特殊字符。 The problem is when I am fetching the value of the cookie in my code behind, the cookie has value as follows:- 问题是当我在后面的代码中获取cookie的值时,cookie的值如下: -

4%2CHealth%20Related%2C%2Fmysite%2FYourKB%2FHealth-Related

I want to get rid of these % signs and values.. Is replacing these characters the only way? 我想摆脱这些%的标志和价值观..正在取代这些人物的唯一途径? How can I make my cookie not have these values and just simple text with some special characters? 如何让我的cookie没有这些值,只是带有一些特殊字符的简单文本?

edit 1 编辑1

I am creating cookie like this now but still the problem persists. 我现在正在创建这样的cookie,但问题仍然存在。 Please help me out. 请帮帮我。

$.cookie('MyCookie', unescape(myString), { path: '/' }, { expires: 30 }); $ .cookie('MyCookie',unescape(myString),{path:'/'},{expires:30});

I know this thread is ancient, but I just ran into this and came across the raw setting . 我知道这个线程是古老的,但我刚遇到这个并遇到了raw设置 Setting it to true fixed the issue for me. 将其设置为true解决我的问题。

For completeness those %xx characters are url encodings of certain characters that are not allowed in URLs. 为完整起见,这些%xx字符是URL中不允许的某些字符的URL编码。

So: %2C is a comma and %2F is a forward slash. 所以:%2C是逗号而%2F是正斜杠。

Taking that into account, where are you checking the value of the cookie again? 考虑到这一点,你在哪里再次检查cookie的价值?

This code works and gives you the correct value as an alert: 此代码有效,并为您提供正确的警报值:

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"> </script>
    <script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
  </head>
  <body>
<script language="javascript">
<!--
$(function(){
  var myString = '4%2CHealth%20Related%2C%2Fmysite%2FYourKB%2FHealth-Related';
  $.cookie('MyCookie', unescape(myString) );
  alert($.cookie('MyCookie'));
    });
-->
</script>
  </body>
</html>

So basically your code above should work, but if you are checking on the server make sure your server-side technology decodes the value as well. 所以基本上你的代码应该可以工作,但是如果你正在检查服务器,请确保你的服务器端技术也解码了这个值。 In ASP.NET this is done automatically when you call Request.Cookies["MyCookie"].Value but AFAIK the browser will transmit it only as a url encoded value so it has to be decoded again on the server. 在ASP.NET中,这是在你调用Request.Cookies [“MyCookie”]时自动完成的。但值AFAIK浏览器只会将其作为url编码值传输,因此必须在服务器上再次解码。

HTH Alex HTH Alex

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

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