doufei4923 2016-06-23 14:06
浏览 41
已采纳

Laravel(5.2)令牌在API请求上不匹配

So I have an interesting issue, as far as I know I am doing things correctly, how ever the token mismatch issue is coming up for API requests.

consider the following:

sendPostData() { let params = { title: this.state.post_title, content: this.state.post_content, tags: this.state.tags, categories: this.state.categories, blog_id: this.props.blogId, };

$.ajax({
  url:      this.props.urlToPostTo,
  type:     'POST',
  header:   {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  },
  data:     params,
  dataType: 'json'
}).done((data) => {
  console.log(data);
});

}

When I console.log($('meta[name="csrf-token"]').attr('content')) I get a token string, so no issue there.

How ever when I post to the URL in question I get:

TokenMismatchException in VerifyCsrfToken.php line 67:

According to these docs I am doing things correctly ... I think.

Ideas?

  • 写回答

2条回答 默认 最新

  • doudu161481 2016-06-23 14:31
    关注

    You should send csrf-token value "headers" block instead of "header" block in ajax like below:

    $.ajax({
        url: this.props.urlToPostTo,
        type: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data: params,
        dataType: 'json'
    }).done((data) => {
        console.log(data);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?