【发布时间】:2021-04-28 14:39:58
【问题描述】:
我正在尝试查看 Laravel + Vue,但我在 vista 方面遇到了问题。 如何通过 axios 正确发送数据? 看看我是怎么做到的。
<button onClick="addVote(question.id)">Vote</button>
data() {
return {
questions:[],
percentages:[],
vote:''
}
},
methods: {
getQuestions(){
axios.get('/questionlist')
.then((response)=>{
this.questions = response.data.question
})
},
getPercentages(){
axios.get('/getpercentage')
.then((response)=>{
this.percentages = response.data.percentage
})
},
addVote(id) {
axios.post(`/vote/${id}`, data);
}
},
created() {
this.getQuestions(),
this.getPercentages(),
this.addVote()
}
这些是视图中接收数据和发送数据的方法。 最后一个是我想向服务器发送请求并能够为每个投票添加一个 + 的投票方法。
这也是控制器
public function vote($id) {
$question = Question::find($id);
if(Auth::user()->votes == 0) {
$question->increment('votes', 1);
$question->increment('totalvotes', 1);
Auth::user()->update([
'votes' => '1'
]);
Alert::success("Success", 'Your vote has been added');
}
return response()->json($question);
}
Route::POST('/vote/{id}', 'QuestionController@vote');
我的问题是,在这些情况下我做错了什么?
【问题讨论】:
-
datainaddVote未定义。如果您想这样做,您需要发布this.$data。 stackoverflow.com/questions/46091418/vuejs-get-data-as-object