提取不会发送,但邮递员会发送


I am working on resrvation system. But when I started to sending data from addForm my fronted won't send them.
I tryied to send POST from Postman and Everything was fine. Before post Success post
When I try to fetch it from form I will be redirect on "/" and server receive nothing. addForm.js

//...
    sendEvent() {
        fetch('/query',
            {
                method: "POST",
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    timeFrom: this.state.timeFrom,
                    timeTo: this.state.timeTo,
                    date: this.state.date,
                    calendarTitle: this.state.userID
                })
            })
            .then(res => res.json())
            .then(qo => {
                    this.setState({addingState: qo})
                    console.log(qo)
                }
            )
    }
render(){
            return (
                <div>
                    <form onSubmit={() => {
                    }}>
                        //Some other input...
                        <button onClick={this.sendEvent}>Submit!</button>
                    </form>
                </div>
            );
}


server.js

//...
app.post('/query', async (req, res) => {
    try {
        let queryInput = (Object.values(req.body).join(' '));
        console.log(`\n  Query input : ${queryInput}`);

        //const result = await dbApi.query(queryInput);
        //console.log(`\n  Query output : ${JSON.stringify(result)}`);

        res.json('Succeed');
    } catch (error) {
        console.error(error);
        res.status(500).json({message: 'Please try again soon'});
    }

评论
  • comnis
    comnis 回复

    您不认为这行以下:

    Because this will send a request to your_react_url/query, and it won't work obviously

    
    fetch('/query',
    

    应该看起来像这样:

    fetch('http://localhost:500/query',