3

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!

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

19 Comments

Still not working. The JS passes '+' to the server, but it still decodes the + as ''
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.
@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.
@oshafran - Excellent. Now that I know what we are dealing with, I have also updated my answer.
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
|
2

javascript:

escape(string);

C#:

Microsoft.JScript.GlobalObject.unescape(string);

this combination works fine for me.

Comments

-1

No Need to do encoding in javascript and decoding in c#. Just use 'encodeURIComponent(string)' javascript function and no need to do any changes in c# code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.