开发者

Problems encoding\decoding strings JS <-> C#

开发者 https://www.devze.com 2023-01-19 12:02 出处:网络
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

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 +'))' />

funct开发者_JS百科ion 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!


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.


javascript:

escape(string);

C#:

Microsoft.JScript.GlobalObject.unescape(string);

this combination works fine for me.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消