javascript - 收到的请求总是空的
问题描述
我正在使用 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;
}
解决方案
推荐阅读
- javascript - 如果应用程序正在侦听“localhost”,我们如何访问网络外部的节点服务器?
- python - 如何将另一个脚本的控制台打印从我的 python 脚本中传输到文件?
- maven - Spark-submit 没有使用我项目的 protobuf 版本
- ios - 在 UIScrollView 中嵌入 UIStackView 时出现的问题
- python - 对 __iter__(self) 和 iter() 感到困惑
- postgresql - 如何获得更改运行时参数的权限?
- linux - 如何将我的主机中的文件夹挂载到自定义 docker 映像,在目录中使用 Dockerfile 进行设置?
- c - C中显示文件记录小问题的功能
- google-play - 无法修复 Eclipse 中的 Libpng 问题
- javascript - html5 音量不变