0

I'm saving an Array Json in cookies from server with something like this:

HttpCookie myCookie = Request.Cookies["ProcessArray"];

myCookie.Value = JSONC.Serialize(lstProcess);

and in Chrome I'm getting this (with javascript):

document.cookie

"ProcessArray=[{"ProcessID":1,"Description":"Adquisición de Articulos","Path":"internalprocess.aspx?process=1"}]"

but in IE (8,9,10) I'm getting this:

document.cookie

"ProcessArray=[{"ProcessID":1,"Description":"Adquisición de Articulos","Path":"internalprocess.aspx?process=1"}]"

What can I do?

Community
  • 1
  • 1
Michael Aguilar
  • 766
  • 8
  • 16

1 Answers1

2

ó is the UTF-8 encoded version of ó

My guess is your JSON serializer is converting to UTF-8. Maybe that can output ISO-8859-1 instead? (see What is the difference between UTF-8 and ISO-8859-1?)

If you are setting the cookie via a Set-Cookie HTTP Header (vs. in JavaScript), IE probably handles this different that Chrome. (see HTTP header should use what character encoding?)

Update: EricLaw's comment is correct about using US-ASCII. I think RFC 2047 is the best reference on this, where it introduces "encoded word" for character sets other than US-ASCII.

However, in this particular example, instead of using encoded word or %XX URL encoding, this cookie value is JSON, so I would use the JavaScript string escape sequence (see Special Characters (JavaScript) and Converting Unicode strings to escaped ascii string). ó is character 0xF3 so use the string '\u00F3' in your JSON formatted cookie value. This allows any client side cookie reads, to just JSON.parse() the value.

document.cookie

"ProcessArray=[{"ProcessID":1,"Description":"Adquisici\u00F3n de Articulos","Path":"internalprocess.aspx?process=1"}]"

Community
  • 1
  • 1
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
  • 2
    For interoperability, HTTP headers should always be pure ASCII. Use %-UTF8 encoding on the cookie name/value before storing it to the headers. – EricLaw May 18 '13 at 04:22
  • @EricLaw - I knew that, but forgot - thanks for reminding me. I updated the answer with more correct info. – Kevin Hakanson May 18 '13 at 13:54