📜  nodejs 中的后端 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:19.877000             🧑  作者: Mango

代码示例2
import React from 'react';
export default class AddBook extends React.Component {

constructor(){
        super();
        this.state = {
            bookname:'',
            author:'',

        };

    }

updateInfo = (event) =>{
        let fieldName = event.target.name;
        let fieldValue = event.target.value;
        if(fieldName === 'bookname') {
            this.setState({bookname: fieldValue});
        }
        else if(fieldName === 'author'){
            this.setState({author:fieldValue});
        }
};

addBook=(e)=>{

let {bookname,author}=this.state;
fetch('localhost:3000/add_book', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        bookname:bookname,
        author:author,
      })
      }).then(response=>response.json()).then(data=>{
      window.alert(data)
      //Do anything else like Toast etc.
})
}

render(){
return(
) } }