【问题标题】:POST (Internal Server Error) using CSRF token使用 CSRF 令牌发布(内部服务器错误)
【发布时间】:2020-01-05 10:26:33
【问题描述】:

在 Jquery ajax 的帮助下,我正在尝试编辑表单中的数据。 根据其他解决方案,我已经在 meta 和 ajax 设置中包含了 csrf 令牌。

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

在标题中

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

jQuery

$.ajax({
  type: "POST",
  url: "{{url('api/edit-data')}}",
  data: {
    id: id,
    name: name,
    contact: contact,
    email: email,
    valley: valley,
    paddress: paddress,
    taddress: taddress,
    qualification: qualification,
    ts: ts,
    experiences: experiences,
    tob: tob,
    es: es,
    level: level,
    ey: ey,
    cn: cn,
    cv: cv,
  },


  success: function(res) {
    console.log(res.sessiondata);
    //  alert('successful');
  }
});

控制器

 public function editdata(Request $request)
    {

        $id = $request->id;
        $data['name'] = $request->name;
        $data['contact'] = $request->contact;
        $data['email'] = $request->email;
        $data['valley'] =$request->valley;
        $data['paddress'] = $request->paddress;
        $data['taddress'] = $request->taddress;
        $data['qualification'] = $request->qualification;
        $data['ts'] =  $request->ts;
        $data['experiences'] = $request->experiences;
        $data['tob'] = $request->tob;
        $data['es'] = $request->es;
        $data['level'] = $request->level;
        $data['ey'] = $request->ey;
        $data['cn'] = $request->cn;
        $data['cv'] = $request->cv;
        if(cv::find($id)->update($data))
        {
            return response([
                'sessiondata' => $data
            ]);
        }else{
        return response([
            'sessiondata'=> $request->name
        ]);
        }

    }

实际错误

POST http://127.0.0.1:8000/api/edit-data 500(内部服务器错误)

已经有很多关于这个错误的解决方案。 我已相应地关注了他们,但我仍然收到此错误。 我也已经在 jquery 的标头中包含了 csrf_token 。 感谢您的帮助。

【问题讨论】:

  • 错误信息是什么?您可以查看storage/logs 目录以获取laravel.logfile 以查看错误的上下文。如果不存在日志,那么您可能有写入权限问题。
  • 我认为您没有因为 CSRF 而出现错误。您的错误必须与其他有关。发布您收到的错误消息。
  • @HarunYilmaz 在我的存储/日志中它说在 null 上调用成员函数 update()
  • 所以你的cv::find($id) 返回null。您可以使用findOrFail 并用try/catch 包围它,并在catch 部分返回正确的响应。

标签: jquery ajax laravel


【解决方案1】:

提交数据时,可以按如下方式添加token:

var name = $("input[name=name]").val();
var password = $("input[name=contact]").val();
var email = $("input[name=email]").val();

... other fields ...

/* I would change here the url() helper by route() with route name */

$.ajax({
    url: "{{url('api/edit-data')}}",
    method: "post",
    dataType : "json",
    data : { _token : "{{ csrf_token() }}", name: name, contact: contact,
                email: email, ...    },
    success:function(data){

          console.log(data.sessiondata);

    }
    error:function(data){

          console.log(data.sessiondata);

    }

});

Cv 是你的模型类吗?如果是这样,在控制器中您可以像这样获取数据:

public function editdata(Request $request)
{
    $data = $request->except('_token');

    $result = Cv::where('id', $id)->update($data);

    if($result)
    {
        return response()->json(['sessiondata' => $data]);
    }
    else
    {
        return response()->json(['sessiondata'=> $request->name], 400);
    }
}

干杯!

【讨论】:

    【解决方案2】:

    这对我有用,试试这个。 var token = "{{csrf_token()}}",

    $.ajax({
      method: "POST",
      url: "{{url('api/edit-data')}}",  
      data: {
        _token:token,
        id: id,
        name: name,
        contact: contact,
        email: email,
        valley: valley,
        paddress: paddress,
        taddress: taddress,
        qualification: qualification,
        ts: ts,
        experiences: experiences,
        tob: tob,
        es: es,
        level: level,
        ey: ey,
        cn: cn,
        cv: cv,
      },
      success: function(res) {
        console.log(res.sessiondata);
        //  alert('successful');
      }
    });
    

    在 ajax 调用之前在变量中生成令牌。

    【讨论】:

    • 如果您的响应数据是 json 则在 AJAX 中添加 ** dataType:'json' **
    猜你喜欢
    • 2018-06-19
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多