Laravel5 CSRF筛选器:返回HTTP 422错误 [英] Laravel5 CSRF Filter: Return HTTP 422 Error

查看:88
本文介绍了Laravel5 CSRF筛选器:返回HTTP 422错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ajax访问服务器时,出现422(不可处理实体)错误.

When I ajax accessed to server, I got 422 (Unprocessable Entity) error.

有人知道如何解决吗?

<meta name="csrf-token" content="{{ csrf_token() }}" />

<script>
      $(document).ready(function(){
          $("#frm").submit(function(e){
              e.preventDefault();
              var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

              $.ajax({
                  type: 'post',
                  url : 'http://example.com/contact',
                  data: {_token: CSRF_TOKEN},
                  dataType: 'JSON',
                  success : function(data){
                      console.log(data);
                  }
              },"json");
          });
      });
</script>

我收到此错误.

http://example.com/contact 422 (Unprocessable Entity)

我通过Google Chrome浏览器检查了表单数据.看来还可以.

I checked form data by Google Chrome. It seems to be OK..

_token:nBfBpE56cFqgmEy94KCji975dZXt3K5MSnlHJT5y


更新


Update

我通过遵循ConvertToInt32的commnet添加了"address"参数,这是必需的参数.然后我又遇到了另一个错误HTTP 403 (Forbidden) error.

I added 'address' parameter which was required parameter by following ConvertToInt32's commnet. And then I got another error which was HTTP 403 (Forbidden) error.

<script>
      $(document).ready(function(){
          $("#frm").submit(function(e){
              e.preventDefault();
              var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
              var address = $("textarea#address").val();

              $.ajax({
                  type: 'post',
                  url : 'http://example.com/contact',
                  data: {'_token': CSRF_TOKEN, 'address' : address},
                  dataType: 'JSON',
                  success : function(data){
                      console.log(data);
                  }
              },"json");
          });
      });
</script>


更新


Update

app \ Http \ Requests

app\Http\Requests

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

abstract class Request extends FormRequest {

    public function authorize()
    {
        // Honeypot 
        return  $this->input('address') == '';
    }

}

ContactRequest

ContactRequest

?php namespace App\Http\Requests;

class ContactRequest extends Request {

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'message' => 'required'
        ];
    }

}

请求参数可能没问题.

_token:nBfBpE56cFqgmEy9wKCjixf5dZXt3K4MSnlHJT3y
address:a

推荐答案

422 是laravel中通过Ajax提交表单时用于无效数据的自然响应代码.并且您的数据必须包含所有已定义的字段在您的请求规则方法中. 403 表示您无权执行该请求.在您的请求类中,有一个名为authorize()的方法.您可以使用此方法检查用户是否有权发出此请求.返回false,则将出现 403 错误.

422 is natural response code in laravel for invalid data when submitting forms via ajax.And your data must include all fields that is defined in your request rules method. 403 means you are not authorized to perform that request.In your request class there is method called authorize().And you can use this method to check if user has an authority to make this request.If this return false then you will get 403 error.

这篇关于Laravel5 CSRF筛选器:返回HTTP 422错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆