0

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 could i decode this characters to normal words? Thanks for help!

1
  • Thanks everybody, all answers are very useful! Commented Apr 15, 2010 at 17:35

2 Answers 2

2

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.

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

1 Comment

which in russian means "not enough letters"
2

Since it's JSON, just parse it

$('input').ajaxSuccess(function(e, xhr, settings) {
    console.log( $.parseJSON( xhr.responseText ) );
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.