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 –