Answer the question
In order to leave comments, you need to log in
Problem with fetch, what should I do?
Created (for training) on a free hosting site type
Uploaded some json there: https://facejsonserver.000webhostapp.com/data.json
And I make a request:
fetch('https://facejsonserver.000webhostapp.com/data.json ')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
Answer the question
In order to leave comments, you need to log in
You can pick up the file only "inside" the hosting. If it is possible to execute PHP, then you can use it to set up CORS and return the contents of the file.
Something like this:
<?php
$filename = "data.json";
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
if (file_exists($filename)) {
print(file_get_contents($filename));
} else {
print(json_encode([]));
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question