0

I got this guy. He decode a string into a readble one.

decodeURIComponent("Invalid Ticker Name \u0027t\u0027 at line: 3")
"Invalid Ticker Name 't' at line: 3"

Now look at this one. It tells us that the source of this string (array item) is really a string.

typeof decodeURIComponent(errorsArr[i])
"string"

But when I try to decode the array item, it just doesn't work.

decodeURIComponent(errorsArr[i])
"Invalid Ticker Name \u0027t\u0027 at line: 3"

What to do?

neoswf
  • 4,730
  • 6
  • 39
  • 59

2 Answers2

1

I know what it is. Your string has a literal \u0027 in it. You can verify this behavior in a browser with:

javascript:alert("\u0027");alert("\\u0027");

When you enter it as:

Invalid Ticker Name \u0027t\u0027 at line: 3

It's substituting out the unicode character. Why can't you use %27?

Wug
  • 12,956
  • 4
  • 34
  • 54
  • Its comming from the browser, as the callback of a json call. I cannot control it, but I can change the string before decoding it. Thank you! I'll guve it a try. – neoswf Aug 07 '12 at 20:40
  • Are you actually using a json parser? they should substitute such sequences out for you. – Wug Aug 07 '12 at 20:42
  • the content type i send is pplication/json. The plugin developer "https://github.com/valums/ajax-upload" said this: If the document was sent as 'application/javascript' or 'text/javascript', then the browser wraps the text in a
     tag and performs html encoding on the contents. But I send correct. I must find where's the problem. Thank you @Wug, you showed me the direction.
    – neoswf Aug 07 '12 at 20:51
1

Problem been solved.

As been written in the code

// If the document was sent as 'application/javascript' or
// 'text/javascript', then the browser wraps the text in a <pre>
// tag and performs html encoding on the contents.  In this case,
// we need to pull the original text content from the text node's
// nodeValue property to retrieve the unmangled content.
// Note that IE6 only understands text/html

I have started serving the file as html/text, and that solved the problem.

neoswf
  • 4,730
  • 6
  • 39
  • 59