Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Yii2 ajax bad request (#400)

$
0
0

When I use this code, I get this error as a response:

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>');            }        }    });});

I tried adding

_csrf: yii.getCsrfToken()

and

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

but that's not working

it does work when I add this to my controller, but that's no good, I don't want to disable csrf validation

public $enableCsrfValidation = false;

How can I fix this?


Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>