Cookie中的字符串(&&符)(javascript) [英] Broken string in cookie after ampersand (javascript)

查看:173
本文介绍了Cookie中的字符串(&&符)(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我从一个cookie读取的字符串在&符号后被破坏。因此,例如字符串hello& world只显示hello。它是一个字符串,它是一个短代码,并使用开关功能转换为更有意义的东西,然后显示在文本框中。开关功能工作正常,但显然如果它没有从cookie读取完整的字符串,那么它将不能在开关函数内找到短代码。



我目前正在使用以下代码读取cookie ...

  document.example.textfield.value = switchFunction(unescape(coalesce($ _ GET ['example'],readCookie('_ cookie'))))); 

如果您需要我提供更多信息,请让我知道。这是我在这里的第一篇文章,所以如果有任何错误或不清楚,提前道歉。



感谢您的帮助。



EDIT b

switchFunction看起来像这样。

  function SwitchFuntion(Code){
switch (Code){
case'text&文本,文本':返回'新的有意义的文本';打破;
}
}


$ b

readCookie函数是这样的...

  function readCookie(name){
var nameEQ = name +=;
var ca = document.cookie.split(';');
for(var i = 0; i var c = ca [i]; $ c $ b while(c.charAt(0)=='')c = c.substring(1,c.length);
if(c.indexOf(nameEQ)== 0)return c.substring(nameEQ.length,c.length);
}
return null;
}


解决方案

在ASP.NET MVC应用程序中的类似问题。



当我将包含&符号的字符串保存到Cookie名称/值对时,它实际上被分成一系列名称/值对



例如尝试保存(value,cookiedata123& book = 2& page = 0)将导致创建3个名称值对value =cookiedata123; book=2;和page=0



我已经通过URL解决了这个问题。一旦我读出来。在.net中调用类似这样:

  //编码
return System.Web.HttpUtility.UrlEncode );

//解码
return System.Web.HttpUtility.UrlDecode(encodedCookieData);

这需要处理任何可能导致问题的&符号,等号等。查看此帖此处的有关cookie中允许的字符的信息。 Cookie中允许的字符


I have a slight problem in that the string I'm reading from a cookie is broken after the ampersand. So for example the string "hello & world" would just display "hello ". It is a string that is a short code, and converted to something more meaningful using a switch function, and then displayed within a text box. The switch function works fine, but obviously if it is not reading the full string from the cookie in the first place, then it won't be able to locate the short code within the switch function.

I am currently using the following code to read the cookie...

document.example.textfield.value = switchFunction(unescape(coalesce($_GET['example'],readCookie('_cookie'))));

If you need me to supply anymore information then please let me know. This is my first post here, so apologies in advance if anything is wrong or unclear.

Thanks For your help.

EDIT

The switchFunction looks like this..

function SwitchFuntion(Code){
    switch(Code){
       case 'text & text, Text' : return 'new meaningful text'; break;
    }
}

etc....

The readCookie function is like this...

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

解决方案

I think I have run into a similar problem in an ASP.NET MVC app.

When I save a string containing ampersands to a cookie name/value pair it actually gets split into a series of name/value pairs

e.g. attempting to save ("value","cookiedata123&book=2&page=0") will result in 3 name value pairs being created "value"="cookiedata123"; "book"="2"; and "page"="0".

I have resolved this by URL Encoding the value just before writing to the cookie and URL Decoding it as soon as I read it out. In .net the calls look like this:

// Encode
return System.Web.HttpUtility.UrlEncode(cookieData);

// Decode
return System.Web.HttpUtility.UrlDecode(encodedCookieData);

That takes care of any ampersands, equals signs etc that can cause problems. See this post here for info on characters that are allowed in cookies. Allowed characters in cookies

这篇关于Cookie中的字符串(&amp;&符)(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆