Yii2 Ajax-schlechte Anfrage (# 400)
Frage
Wenn ich diesen Code verwende, erhalte ich diesen Fehler als Antwort:
generasacodicetagpre.schlechte Anforderung (# 400): Ihre Daten nicht möglich, um Ihre Daten zu überprüfen
Ich habe versucht, das Hinzufügen von
hinzuzufügen_csrf: yii.getcsrftoken ()
und
contenttype: "application / json; charset= utf-8",
Datentyp: "JSON",
aber das funktioniert nicht
Es funktioniert, wenn ich dies meinem Controller hinzufüge, aber das ist nicht gut, ich möchte die CSRF-Validierung nicht deaktivieren
public $ enablecsrfValidation= FALSE;
Wie kann ich das beheben?
Lösung 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>');
}
}
});
});
Andere Tipps
Sie können diesen Weg ausprobieren.Es ist Arbeit!
generasacodicetagpre.Andere Beispiele: http://docs.mirocow.com/doku.php?id=yii2:docs#добавление_csrftoken_в_ajax_запрос_yii2
Fügen Sie diesen Code am Ende Ihres Layouts hinzu:
generasacodicetagpre.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"