N
N
NooBick2021-11-05 15:40:41
Node.js
NooBick, 2021-11-05 15:40:41

Error after fetch?

fetch("https://vk.com/foaf.php?id=1", {mode: 'no-cors'})
        .then(res => res.text())
        .then(str => new DOMParser().parseFromString(str, "text/xml"))
        .then(data => console.log(data))


In response:

<html xmlns="http://www.w3.org/1999/xhtml"><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Extra content at the end of the document
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror><body></body></html>


If you use the cors method, it will generally give an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-11-05
@Aetae

fetchdoes not know how to encode other than utf-8, and there windows-1251.
Either use the classic xmlhttp, which will parse xml for you at the same time, or add work with ArrayBuffer and TextDecoder to the chain.
Well, of course, you should have the right to make such a request. From a random site, you cannot get data from vk.com unless you are allowed to. 'no-cors'here it means only that the request will reach there , but no one will give you an answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question