在没有表格的情况下发送自动发帖请求吗?

时间:2019-09-10 16:39:25

标签: javascript reactjs api post

通常,您通过表单上的“提交”按钮发送发帖请求。但是,我需要发送不带表格的发帖请求。实际上,我需要在用户为患者选择位置和状态后立即发送发布请求。

我尝试在handleAutoObsSubmit()上发布componentDidUpdate(),但是每次有DOM更新时都会发送此发布请求,即单击其他按钮,我只希望它发送一次。

发送这样的发帖请求还可以吗?

    handleAutoObsSubmit = () => {

       const postObsUrl = '/observation/record';
        this.state.patientInfo.forEach(patient => {
            if (patient.locationInfo !== '' && patient.status !=='') {
                const data = {
                    room: patient.room,
                    patient: patient.name,
                    location: patient.locationInfo,                  
                    status: patient.status,
                };
                fetch(postObsUrl, {
                    method: 'POST',
                    body: JSON.stringify(data),
                    headers: {
                        'Content-Type': 'application/json'
                    }
                })
                    .then((res) => {
                        console.log(data);

                        if (!res.ok) {
                            console.log('request failed');
                        } else {
                            console.log('request sent');
                        }
                    });
            }
        });
}
  patientInfo = [
        { count: 959999,  room: "1", name: 'John Nero', locationInfo: '', status: ''},
        { count: 959999, room: "2", name: 'Shawn Michael', locationInfo: '', status: ''},
        { count: 959999, room: "3", name: 'Gereth Macneil', locationInfo: '', status: ''}
]

0 个答案:

没有答案