简体   繁体   English

POST 400错误,ajax请求

[英]POST 400 error, ajax request

Why cant I save this, I get 400 (Bad Request), and on headers response i get The CSRF token could not be verified. 为什么我不能保存它,却得到400(错误请求),并且在标头响应上却得到CSRF令牌无法验证。

 $(document).ready(function() {

            $("a#copylink").click(function (e) {
                e.preventDefault();
                var data = $('#campaign-form').serialize();

                $.ajax(
                    {
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        method: 'POST',
                        url: 'campaignsave',
                        data: data,
                        success: function(data){
                            alert(data);
                        }
                    }
                )
                    });
            });

on backend: 在后端:

public function actionCampaignSave()
    {
        var_dump($_POST);

    }

You can pass the [headers] parameter in your ajax call like this. 您可以像这样在ajax调用中传递[headers]参数。

$.ajax({
    url : 'campaignsave',
    method : 'POST',,
    headers : {
        'X-CSRF-TOKEN' : $('input[name="token"]').val()
    }
    dataType : 'json',
    data : data,
    success : function(response) {
        console.log(response);
    }
});

Just make sure you've place {!! 只要确保您放置了{!! csrf_field() !!} on your [view] blade template to append the $(input[name="token"); csrf_field()!!}在您的[view]刀片模板上,以附加$(input [name =“ token”); html tag to get the token value to get the CSRF token as well. html标记以获取令牌值,以获取CSRF令牌。 Hope this helps 希望这可以帮助

Try this it may help you 试试这个可能对您有帮助

 $.ajax({
                    type: "POST",
                    url: 'campaignsave',
                    data: {test : data},
                    success: function(data) {
                         alert(data);
                    }
                });

Pass csrf token using headers property on ajax 在ajax上使用headers属性传递csrf令牌

$.ajax({
  contentType: "application/json; charset=utf-8",
  dataType: 'json',
  method: 'POST',
  url: 'campaignsave',
  headers: { 'X-CSRF-TOKEN': 'token' }
  data: data,
  success: function(data){
      alert(data);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM