Answer the question
In order to leave comments, you need to log in
Why does it output HTTP status 0 when calling fetch?
Good afternoon, I'm trying to get a JSON object from this site https://market.csgo.com/history/json/
I'm
moving on myself - the data is there. I'm trying to get them through fetch, if you call fetch - with the no-cors mod, then it gives 0 http status, if with the cors mod, then it says No 'Access-Control-Allow-Origin' header is present on the requested resource.
As I read, you can set Access-Control-Allow-Origin only on the server, that is, if I set them, nothing will happen. But even if they put them, nothing will change. I understood these requests very badly, please explain what is wrong ... I kind of read the documentation about fetch and headers, but it doesn’t reach me what I’m doing wrong
const requestURL = 'https://market.csgo.com/history/json/'
async function displayItems(url) {
await fetch(url, {
mode:'no-cors'
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP status " + response.status)
}
return response.json()
})
.then((message) => {
console.log(message)
})
.catch((error) => {
console.log('Что-то пошло не так ' + error)
});
}
displayItems(requestURL)
Answer the question
In order to leave comments, you need to log in
Can't get response in no-cors mode
https://fetch.spec.whatwg.org/#concept-request-mode
"no-cors"
Restricts requests to using CORS-safelisted methods and CORS-safelisted request-headers. Upon success, fetch will return an opaque filtered response .
An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list, status is 0 , status message is the empty byte sequence, header list is empty, and body is null.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question