简体   繁体   中英

Use ReactJS and Axios to give data to API in PHP

I'm working on creating a basic ReactJS Frontend App that has to send data to an API created with php. My react app is hosted on localhost:3000 using XAMPP and the php file is hosted on localhost:8000. when I try to connect to it I get a connection refused error. I use Axios to send a Post request. I know this might be very general but any ideas to how to solve this. or go about programming that App?

This is what it currently is. I get a net::ERR_CONNECTION_REFUSED whenever I call it axios.post('http://localhost:8000/file.php', someData)

You may need to add a proxy in your package.json file:

"proxy": "http://localhost:8000"

Read more: https://docs.npmjs.com/misc/config#proxy

Although I'm not sure there's a config option in Axios to specify port, you should be able to send a post request to that port like this:

axois.post('http://localhost:8000')
    .then((success) => {
        console.log(success)
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM