2017-05-07 105 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

相关问题