My python server uses json.dumps() to dump the json object into the string, but it also converts the binary utf-8 code which is not ascii, into the stringized thing, like "\u4e2d". So what my client see is this string, is there any api to convert "\u4e2d" back to utf-8 character?
In jQuery you can use $.parseJSON to decode a JSON-formatted object. That includes decoding encoded characters. Using dataType: 'json' with $.ajax (or $.post) or using $.getJSON will do that for you if you are trying to make AJAX requests.
Related
What is best way to send data from html to webService? My base64 string is very long (5000 character and more).
I'm use ajax but not working for very long string.
Any solution?
I am sending the return url as a query string parameter like this http://localhost:50316/TripNestFinal/Login.aspx?ReturnUrl=~/Account/AccountSettings.aspx
But i want ~/Account/AccountSettings.aspx to be encoded in such a way so that i can
encode/decode in jQuery/javascript
encode/decode in VB.NET
encode in VB.NET and decode in javascript/jQuery and vice versa
This doesn't have to be bulletproof as i am not dealing with security here. All i want to do is to change this ~/Account/AccountSettings.aspx to something that does not show the path directly.
I thought of using Base64 encoding but when i Base64 Encode , it includes a '/' character which breaks my jQuery logic. Is there any way i can avoid the '/' character when i Base64 encode?
You will need to URL encode the Base64 encoded string, that will work - although you'll need to check the generated query string length to make sure it doesn't hit the .NET maximum.
But I have to ask why you'd want to encode it in this way?
I created a JSONP function on the server and returns a UTF-8 encoded json object like this
applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"});
on my javascript on the client side, i want to convert the garbled part to their original state like
{"Name":"名前", "Age":"年齢"}
I tried $.parseJSON() but it doesnt work
You can use decodeURIComponent to decode urlencoded strings like yours
decodeURIComponent('%E5%90%8D%E5%89%8D');
//result: '名前'
You could use the decodeURIComponent function. But you shouldn't be URL encoding your javascript strings. You should send them as UTF-8 strings as-is. Javascript is capable of understanding them.
I've been looking for the functions [used to encode and decode URL] that can be exchangable between PHP and Javascript [I can encode the URL in javascript and decode it in PHP and vice versa], there are many solutions come up, like escape() and unescape(); encodeURI() and decodeURI() for javascript, or urlencode() and urldecode() for PHP, but there is no pair which can works together perfectly. Because I want to send post data using Ajax jQuery for a PHP page to use, and data can be mixed with special character, that's why I need a function that works the same in PHP and Javascript:
Example:
$.ajax{url: "test.php", data: "http://www.thisURLContainSpecialCharac.ter"}
See the code above? It contains special characters in data, that may cause problem.
I appreciate any advice :). Thanks.
[x]
You don't want to use Javascript's escape function since it does not URL encode. See http://xkr.us/articles/javascript/encode-compare/ for examples where it fails.
Javascript's encodeURIComponent will encode assuming UTF-8, so if you use that and make sure that your request has a content-type of UTF-8, you should be able to decode in PHP using
utf8_decode(urldecode($yourString)).
How do I pass a a large string containing characters like '%' and '&'
to my php page through ajax post?
In other words how to javascript-encode them and php-decode them?
the encodeURIComponent() JavaScript function can be used to escape any of those characters in either the keys or the values.
PHP will receive and decode it automatically into the $_POST array.
The format of the data should be Query String format, specifically:
Content-Type: application/x-www-form-urlencoded
For example:
Name=Joe&Age=23&City=Altoona
If you use encodeURIComponent() on each key and each value, then join them with =, and group them by &, you won't have any further issues.
To encode them in javascript, use encodeUri and encodeUriComponent
http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp
PHP Decode:
http://php.net/manual/en/function.urldecode.php
You actually don't need to encode or decode something, if you use POST parameter in send method of XMLHttpRequest. Only GET needs such and encoding, since it uses URI field.
You should be careful when using this data in SQL requests. Beware of injections.