문제

이 코드를 사용할 때이 오류가 응답으로 얻을 수 있습니다.

잘못된 요청 (# 400) : 데이터를 확인할 수 없음

/**
 * 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 : "응용 프로그램 / json; charset= utf-8",
데이터 유형 : "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>');
                }
            }
        });
    });

다른 팁

이런 식으로 시도 할 수 있습니다.그것은 일입니다!

var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
         url: 'request',
         type: 'post',
         dataType: 'json',
         data: {param1: param1, _csrf : csrfToken},
});
.

  $.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

레이아웃 하단 에이 코드를 추가하십시오.

<script>
    $.ajaxSetup({
        data: <?= \yii\helpers\Json::encode([
            \yii::$app->request->csrfParam => \yii::$app->request->csrfToken,
        ]) ?>
    });
</script>
.

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"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top