Answer the question
In order to leave comments, you need to log in
Why doesn't e.preventDefault() work in Yii2?
Form in view:
<?php $form = ActiveForm::begin(['id' => 'test_form']) ?>
<?= $form->field($menu, 'name') ?>
<?= $form->field($menu, 'age') ?>
<?= Html::submitButton('Click me...', ['id' => 'btn']) ?>
<?php ActiveForm::end() ?>
let form = document.querySelector('#test_form');
const csrfToken = document.querySelector("[name='csrf-token']").content
form.addEventListener('submit', function(e) {
e.preventDefault();
let promise = fetch('/?r=post/show', {
method: 'POST',
body: new FormData(this),
headers: {
"X-CSRF-Token": csrfToken // Set the token
}
});
promise
.then(
data => {return data.json()}
)
.then(
result => {console.log(result)}
)
});
new Vue({});
. The reasons for this behavior are still not clear to me.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question