2013-03-04 40 views
0

我想在asp.net mvc中將國家字符保存到cookie中,然後用javascript顯示它們,但是當我保存這句話時:「Přílišžluťouličkýkůňúpělúděsné託尼「。我收回了這個質量「PĹ™ĂliĹĹžťliliýýĹĹpppppdddĂĂĂĂĂĂĂĂny」。 我使用這個建設節約數據:國家字符 - 保存並從cookie中解析ASP.NET MVC + JS

HttpContext.Current.Response.Cookies.Add(new HttpCookie(string.Format("Flash.{0}", notification), message) { Path = "/" }); 

而這種閱讀:

function setFlashMessageFromCookie() { 
    $.each(new Array('Success', 'Error', 'Warning', 'Info'), function (i, alert) { 
     var cookie = $.cookie("Flash." + alert); 

     if (cookie) { 
      options.message = cookie; 
      options.alert = alert; 

      deleteFlashMessageCookie(alert); 
      return; 
     } 
    }); 
} 

我怎樣才能解決這個問題? 謝謝你們!

回答

1

我會在base64或urlencoding中對它進行編碼,然後在javascript端進行解碼。

0

使用asp.net,嘗試尋找System.Web.HttpUtility來編碼和解碼數據。

HttpUtility.UrlEncode(data); // encoding 
HttpUtility.UrlDecode(encodedData); // decoding 

或嘗試:

function setCookie(name,value) 
{ 
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 

function getCookie(Name) { 
    if (document.cookie.length > 0){ 
     offset = document.cookie.indexOf(Name +"="); 

     if (offset != -1) { // if cookie exists 
      begin += Name.length+1; 
      end = document.cookie.indexOf(";", offset); 

      if (end == -1) 
       end = document.cookie.length; 

      return unescape(document.cookie.substring(begin, end)) 
      } 
    } 
}