2017-05-07 103 views
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); 
    } 

}

+0

它將如果你能很好發佈頁面給出的錯誤。這可能是一個CSRF錯誤,或者你忘記使用Auth或許多其他的東西。 (按f12並去網絡) –

+0

它說:「TokenMismatchException在VerifyCsrfToken.php行68:」 – hxwtch

+0

看一下這些問題的一些答案: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 –

回答

0

您需要添加CSRF令牌: https://laravel.com/docs/5.4/helpers#method-csrf-token

例如,在視圖

<script> 
    var csrf = '{{csrf_token()}}'; 
</script> 

而在你的腳本

var formData = new FormData(); 

formData.append('_token', csrf); 
formData.append('input', this.newUserVoted); 

this.$http.post('http://www.nmdad2-05-elector.local/api/v1/electionuser', formData) 
+0

謝謝,但它沒有奏效。我仍然得到相同的錯誤... – hxwtch

相關問題