Контекст: я хочу загрузить html-файл в реагирующее приложение и отобразить его часть в окне справки. HTML-файл можно обновить в любое время из какого-либо генератора контента, поэтому переводить его в jsx не вариант.
Прямо сейчас я пытаюсь загрузить любой файл (включая json) с помощью axios и получаю ответ 404. Я помещаю вызов в componentWillMount, componentDidMount, что угодно. Я добавил в проект файлы toto.html и toto.json. Я создал приложение, используя приложение create-реагировать
import React, {Component} from 'react';
import axios from 'axios';
class Toto extends Component {
componentWillMount () {
console.log("inside componentWillMount")
axios
.get('./toto.json')
.then(response =>
{ console.log("json response=",response); })
.catch( error => {
console.log("json error=",error);} );
axios
.get('./toto.html')
.then(response =>
{ console.log("html response=",response); })
.catch( error =>
{ console.log("html error=",error);} );
console.log("inside componentWillMount 2")
};
render() {
return (
<div>
<h2>Toto TIti Tata</h2>
<object type="text/html" data="./toto.html" ></object>
</div>
)
}};
export default Toto;
Ответ:
inside componentDidMount Toto.js:11
inside componentDidMount 2 Toto.js:30
html error= Error: "Request failed with status code 404"
createErrorhttp://localhost:3000/static/js/bundle.js:1555:15settlehttp://localhost:3000/static/js/bundle.js:1721:12handleLoadhttp://localhost:3000/static/js/bundle.js:1081:7 Toto.js:27
json error = Error: "Request failed with status code 404"
./toto.json
? должно быть/toto.json
- person Arup Rakshit   schedule 23.08.2018/toto.json
вместо./toto.json
ничего не меняет. Я поставил./toto.json
изначально, потому что файл был в той же папке, что и компонент. - person Francis   schedule 23.08.2018axios
. Вам нужно настроить небольшой экспресс-сервер, а затем, используя открытый API, вы можете прочитать его с помощью axios. - person Arup Rakshit   schedule 23.08.2018