jquery ajax encoding problem!
i send ajax requests with jquery, and i have a function:
$('input').ajaxSuccess(function(e, xhr, settings) {
console.log(xhr.responseText);
});
Ajax response ara russian letters in utf-8, all server (apache, php) and files are in utf-8 but response text is something like this:
\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u043e \u0431\u0443\u043a\u0432!
how c开发者_如何学Goould i decode this characters to normal words? Thanks for help!
That's a JavaScript string literal. Are there quotes around it? There should be:
"\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u043e \u0431\u0443\u043a\u0432!"
is absolutely identical to the literal:
"Слишком мало букв!"
Assuming there are quotes around it, I would expect that this is a JSON response. The normal ajax handler would generally expect jQuery to decode that for it automatically (but I don't know where you're calling that).
If you really do need to decode a JSON string from an XMLHttpRequest object manually like this you can do it by calling:
var s= $.parseJSON(xhr.responseText);
or, if there are really no quotes around it:
var s= $.parseJSON('"'+xhr.responseText+'"');
though if it is not quoted that would raise the question of how the quote character is handled in the response. I would hope it's already escaped for using in a string literal, but it's impossible to tell from the little information here.
Since it's JSON, just parse it
$('input').ajaxSuccess(function(e, xhr, settings) {
console.log( $.parseJSON( xhr.responseText ) );
});
精彩评论