طلب سيء YII2 AJAX (# 400)
سؤال
عندما استخدم هذا الرمز، أحصل على هذا الخطأ كرد فعل:
giveacodicetagpre.طلب سيئ (# 400): غير ممكن للتحقق من بياناتك
حاولت إضافة
_csrf: yii.getcsrftoken ()
و
contenttype: "تطبيق / json؛ Charset= UTF-8"،
Datatype: "JSON"،
ولكن هذا لا يعمل
يعمل عندما أضيف هذا إلى جهاز التحكم الخاص بي، لكن هذا ليس جيدا، ولا أريد تعطيل التحقق من صحة CSRF
Public $ enablecsrfvalidation= false؛
كيف يمكنني إصلاح هذا؟
المحلول 4
This is my code now, just ignore the csrf token:
$(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},
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>');
}
}
});
});
نصائح أخرى
يمكنك تجربة بهذه الطريقة.إنه عمل!
giveacodicetagpre.أمثلة أخرى: http://docs.mirocow.com/doku.php؟id=yii2:docs#добавление_csrftoken_В_ajax_запрос_yii2
أضف هذا الرمز في أسفل التخطيط الخاص بك:
giveacodicetagpre.In my case i've solved this problem with blocking csrf-verification for "site/save-order" route (actionSaveOrder).
class SiteController extends Controller {
...
public function beforeAction($action) {
$this->enableCsrfValidation = ($action->id !== "save-order"); // <-- here
return parent::beforeAction($action);
}
}
i had the same issue
but i noticed that i forgot to add Html::csrfMetaTags()
in the head section and that actually fixed it for me
best of luck
Maybe your AJAX header "Content-Type"
need to change on "application/x-www-form-urlencoded; charset=UTF-8"