0
因此,我试图将数据存储在我的election_users表中,但发出POST请求时会出现“500(内部服务器错误)”错误。我的对象确实返回了正确的“election_id”。我不知道我做错了什么。Post方法失败,Vue.js“500(内部服务器错误)”
这是我在我的Vue脚本方法:
methods: {
createVote: function() {
var itemId = this.$route.params.id
var input = this.newUserVoted
this.newUserVoted.election_id = itemId
this.$http.post('http://www.nmdad2-05-elector.local/api/v1/electionuser', input)
.then((response) => {
this.newUserVoted = {'election_id': itemId}
}).catch(e => {
console.log(e)
console.log(input)
})
}
},
我UsersController.php
public function store(Request $request)
{
$userVoted = new ElectionUser();
$userVoted->election_id = $request['election_id'];
$userVoted->user_id = Auth::id();
if ($userVoted->save()) {
return response()
->json($userVoted);
}
}
我routesApi.php
Route::post('electionuser', '[email protected]');
我ElectionUser.php
class ElectionUser extends Model
{
// Relationships
// =============
protected $fillable = [
'election_id',
'user_id'
];
public function election()
{
return $this->belongsToMany(Election::class);
}
}
它将如果你能很好发布页面给出的错误。这可能是一个CSRF错误,或者你忘记使用Auth或许多其他的东西。 (按f12并去网络) –
它说:“TokenMismatchException在VerifyCsrfToken.php行68:” – hxwtch
看一下这些问题的一些答案:http://stackoverflow.com/questions/27944512/laravel-api-tokenmismatchexception http ://stackoverflow.com/questions/37383165/tokenmismatchexception-for-api-in-laravel-5-2-31 http://stackoverflow.com/questions/32118400/tokenmismatchexception-in-verifycsrftoken-laravel-5-1 –