Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch returns html source of the my own index.html

I am trying to use fetch in an react-create-app server(localhost:3000) to get a static .json file from my apache(localhost:80) but it returns source of my react index.html file!

Specifying port number results in "networking error"

const that=this;
fetch("localhost/myapp/data/structure.json").then((res)=> {return res.text()})
                .then(((data)=>{that.setState({structure: data})})).catch((e)=>{alert(e.toString())});
like image 911
saiedmomen Avatar asked Jun 30 '17 17:06

saiedmomen


Video Answer


2 Answers

Downright the problem comes to making react-create-app work with a local server which is explained in this guide https://daveceddia.com/create-react-app-express-backend/

In short I needed to put a proxy property with the value equal to address of my local server in my package.json. In my case:

  "proxy": "http://localhost:80"
like image 82
saiedmomen Avatar answered Sep 23 '22 20:09

saiedmomen


Try a fully qualified URL:

const that=this;
fetch("http://localhost/myapp/data/structure.json").then((res)=> {return res.text()})
                .then(((data)=>{that.setState({structure: data})})).catch((e)=>{alert(e.toString())});

Note the http://

like image 36
R. Tero Avatar answered Sep 24 '22 20:09

R. Tero