jquery Laravel 5.2使用Select2 v 3.5.1 AJAX 方法POST,令牌不匹配

wljmcqd8  于 2023-04-05  发布在  jQuery
关注(0)|答案(1)|浏览(83)

我正在尝试使用Select2,使用类型:POST并尝试像 AJAX 请求一样通过头部传递令牌。但我得到了TokenMismatchException。但我已经尝试使用Select2使用GET,它工作正常。是否有可能使用Select2与类型POST或只是GET?这是我的代码

var token = '{{csrf_token()}}';
$(".eta").select2({
    placeholder: "Search for source",
    minimumInputLength: 1,
    ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
        url: '{{route("part", "source")}}',
        dataType: 'json',
        type: 'post',
        headers: {
            'X-CSRF-TOKEN': token
        },
        quietMillis: 250,
        data: function (term, page) {
            return {
                q: term, // search term
            };
        },
        results: function (data, page) { // parse the results into the format expected by Select2.
            console.log(data);
            dataSourceParts = [];
            $.each(data, function(key, val) {
                dataSourceParts.push({
                    id: val.id,
                    text: val.name+'-'+val.code
                });
            });
            // since we are using custom formatting functions we do not need to alter the remote JSON data
            return { results: dataSourceParts };
        },
        cache: true
    },
});
dced5bon

dced5bon1#

这是我的工作:

headers: {
  'X-CSRF-TOKEN': '{!! csrf_token() !!}'
},

只需添加引号。并生成带有原始刀片参数的令牌,不会被{!!!}转义。

相关问题