首页 > 解决方案 > 收到的请求总是空的

问题描述

我正在使用 react、redux-form 和 laravel。我创建了一个能够在数据库中插入注释的表单,但是当我Request在 laravel 中显示时,总是会出现一个空数组。我不知道我做错了什么。

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      notes: [],
    };

    this.submitToServer = this.submitToServer.bind(this)
    this.submit = this.submit.bind(this)
  }

  componentWillMount() {
      fetch('http://127.0.0.1:8000/notes')
        .then(res => res.json())
        .then(json => json.results)
        .then(notes => this.setState({ notes }))
        .catch(console.log)
  }

  async submitToServer(data) {
    let response = await fetch('http://127.0.0.1:8000/notes', {
      method: "POST",
      headers: {
        'Accept': 'application/json',
        'Content-type': 'application/json',
      },
      body: JSON.stringify(data)
    })
    let responseJSON = await response.json()
    return responseJSON
  }

  submit({ title, content }) {
    this.submitToServer({ title, content })
      .then(res => this.setState(prevState => ({
        notes: [...prevState.notes, {
          id: prevState.notes.pop().id + 1,
          title: title,
          content: content
        }]
      })))
  }

  render() {
    if (this.state.notes.length > 0) {
      return (
        <div>
          <h1>Notas</h1>
          <Paper>
            <form onSubmit={this.props.handleSubmit(this.submit)}>
              <Field name="title" label="Title" component={renderField} type="text" />
              <Field name="content" label='Content' component={renderField} type="text" />
              <button type="submit">Submit</button>
            </form>
          </Paper>
          ))}
        </div>
      )
    } else {
      return <p>Cargando notas...</p>
    }
  }
}

目前在 laravel 中,我只是返回请求以显示它包含的内容。

public function storeNote(Request $request) {
        return $request;
    }

标签: javascriptreactjslaravelreduxredux-form

解决方案


推荐阅读