views:

44

answers:

1

Hi all,

I have a problem in JS encoding and then decoding in the C# server. I use javascript encode() function - but when i have special chars like +, the C# has httputility.urldecode() -> and it converts it as if it was SPACE char.

What is the best way to communicate JS encoding and C# decoding?

I have <a href='javascript:foo(escape('hello +'))' />

function foo(data)
{
$.ajax({ url: "http:/....." + data, dataType: 'html', context: document.body
...
...
}

I debugged the server, and I get 'hello++' - it doesnt know which + is which (space or +) Thank you!

+2  A: 

Javascript encode does html encoding. Since + is valid in HTML, it does nothing to the +.

However, you are passing this string through the URL - + on a URL means an encoded space.

You need to use the javascript encodeURIComponent if you want the + to be encoded correctly for consumption on the server side:

<a href='javascript:foo(encodeURIComponent('hello +'))' />

You need to understand that HTML encoding and URL encoding are different things.

Oded
Still not working. The JS passes '+' to the server, but it still decodes the + as ''
oshafran
How does it pass the `+` to the server? You should probably encode the `+` before passing it in, as in a URL + _is_ the encoding for a space.
Oded
I do escape("blalblba+")
oshafran
@oshafran - and how do you pass that to the server? Please edit your question and update it with all the details, as answers may get deleted and people don't always read comments on other answers.
Oded
I updated. Thanks
oshafran
@oshafran - Excellent. Now that I know what we are dealing with, I have also updated my answer.
Oded
What about all the other special chars ('@~ etc...)? I wanted to know what is the encoding\decoding relationship between the JS and C# - which means, how do i encode in JS and how to decode in c# in the same algorithm
oshafran
@oshafran - HTML and URLs are different things. `encodeURIComponent` will deal with encoding everything on the URL that need to be encoded.
Oded
I tried it. I still get hello++ and not one plus. I mean when debugging the Request object I can see that
oshafran
@oshafran - What do you get from `HttpUtility.UrlDecode`?
Oded
oshafran
@oshafran - Try putting a `alert` and see what the result of `encodeURIComponent` looks like.
Oded
Wierd... it alerts "Hello +". In the HTML it is embedded as hello%20%2B, but in the alert its Hello +
oshafran
@oshafran - on FF for me it alerts `hello%20%2B`. Make sure you are making the calls you think you are...
Oded
For some reason, if I do the encoding in the foo function it alerts encoded, if its in the href - it won't alert the encoded. anyhow, it still doesnt work
oshafran
@oshafran - Does the data get encoded correctly on the ajax call (use firebug or fiddler to see what _is exactly_ sent on the URL)?
Oded
http://aaaaaaa.aspx?blaaaaa=hello%20+
oshafran
@oshafran - And in C#, what do you see for the RawURL?
Oded
same... blaaaaa=hello%20+
oshafran
@oshafran - So it looks like the server is getting the data properly. Where exactly are you now having the problem? Don't look at the debugger, as it will do funny encoding things, but the actual result.
Oded
It seems like the URIencode isn't transferred to the server (as the alert showed me, the hello%20%2B
oshafran
Oded, I think I solved it. I moved the encodeURI to be inside the foo. and it sends now well, and i HtmlDecode it well. Can there be other chars that will make me problems?
oshafran
Oded
Thank you very much for you time. (nice name... mine it Oded too :))
oshafran
@oshafran - lol... Walla!
Oded
Walla walla....
oshafran