gpt4 book ai didi

php - Yii2 ajax 错误请求 (#400)

转载 作者:可可西里 更新时间:2023-10-31 23:02:37 27 4
gpt4 key购买 nike

当我使用此代码时,我收到此错误作为响应:

Bad Request (#400): Not possible to verify your data

/**
* Active toggle
*/
$(document).on('click', '[data-toggle-active-menu-items]', function(e){

e.preventDefault();

var id = $(this).data('toggle-active-menu-items');

$.ajax({
url: 'active',
type: 'POST',
data: {'id': id, _csrf: yii.getCsrfToken()},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
if (data.active == 1)
{
$('#list-' + id + ' [data-toggle-active-menu-items]').html('<span class="glyphicon glyphicon-eye-open"></span>');
} else {
$('#list-' + id + ' [data-toggle-active-menu-items]').html('<span class="glyphicon glyphicon-eye-close"></span>');
}
}
});
});

我尝试添加

_csrf: yii.getCsrfToken()

contentType: "application/json; charset=utf-8",
dataType: "json",

但这行不通

当我将它添加到我的 Controller 时它确实有效,但这不好,我不想禁用 csrf 验证

public $enableCsrfValidation = false;

我该如何解决这个问题?

最佳答案

  $.ajax({
url: '$urlSave',
type: 'post',
data: {payload: payload, _csrf: yii.getCsrfToken()},
dataType: 'json',
}).success(function(response) {
});

其他示例:http://docs.mirocow.com/doku.php?id=yii2:docs#добавление_csrftoken_в_ajax_запрос_yii2

关于php - Yii2 ajax 错误请求 (#400),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054945/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com