簡體   English   中英

POST 方法不起作用 ReactJs、NodeJs、ExpressJs

[英]POST method not working ReactJs, NodeJs, ExpressJs

錯誤->
POST 方法錯誤

上面的 post 方法不起作用,但類似的 get 方法版本正在起作用

我有以下代碼:
應用程序.js

  • 與后端通信
handleData = async (url) => {
    const newData = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        },
        body: JSON.stringify({
            id_: this.state.id,
            firstName: this.state.firstName,
            lastName: this.state.lastName,
            email: this.state.email,
            password: this.state.password,
        }),
    }).then(res => res.json());

    console.log('newData-', newData);
    return newData;
}
  • 形式
<form action='/mainApp' method='GET' onSubmit={this.handleSubmit} id="signUpForm">

服務器.js

app.post('/mainApp/show', async (req, res) => {
    console.log('Show called');
    const result = await dbOperation.showVals(req.body.name);
    res.send(result.rows);
});

app.post('/mainApp/insert', async (req, res) => {
    console.log('Insert called');
    const result = await dbOperation.showVals('email', req.body.email);
    console.log(result.rows.length);
    if (result.rows.length > 0) {
        console.warn('Duplicate email address');
        res.send({ errorPresent: true, errorMessage: 'Duplicate email address' });
    } else {
        console.log('Inserting', req.body.id_, req.body.firstName, req.body.lastName, req.body.email, req.body.password);
        await dbOperation.insertVals(req.body.id_, req.body.firstName, req.body.lastName, req.body.email, req.body.password);
        res.send({ errorPresent: false, errorMessage: 'No error' });
    }
});

我嘗試將所有轉換為 GET 並且它正在工作

它適用於 get 方法,因為您正在發送 GET req

<form action='/mainApp' method='GET' onSubmit={this.handleSubmit} id="signUpForm">

對於 POST req,您必須將表單內的方法更改為

<form action='/mainApp' method='POST' onSubmit={this.handleSubmit} id="signUpForm">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM