M
M
mi_ka2021-10-04 08:10:44
JavaScript
mi_ka, 2021-10-04 08:10:44

fetch(). Answer with Unicode Character. How to "decode"?

Hello.

I make a request to a server (not mine) using fetch().
This is how I get the answer:
await response.text();
The answer contains: blotches jQuery + html layout + useful information about the movie. In the response (Latin and Cyrillic, which is represented by Unicode Character. A small piece \u0417\u0430\u043a\u0440\u044b\u0442\u044c) I

assign the response to a variable to parse the information using jQuery, and here the problems begin, because js does not perceive characters \uXXX.

tried

decodeURIComponent(await response.text());
JSON.parse(await response.text());

also received the response as response.arrayBuffer() and then used TextDecoder(), but all without success.

at the same time, if you manually assign the value "\u0417\u0430\u043a\u0440\u044b\u0442\u044c" to the variable and display this variable via alert(), then everything is fine, the text is displayed correctly ("Close").

I suspect the problem is in how fetch() provides the response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2021-10-04
@zkrvndm

Look in what encoding the server gives you a response, to do this, open the browser console, go to the Network tab and take a screenshot of the received headers, then upload it here.

R
Rsa97, 2021-10-04
@Rsa97

For JSON.parse, you need to surround such a string with double quotes in order to form the correct JSON.

const encoded = '\u0417\u0430\u043a\u0440\u044b\u0442\u044c';
const decoded = JSON.parse('"' + encoded + '"');
console.log(decoded); // Закрыть

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question