[英]Ajax form submit in yii framework
I am sure the problem is with ajax. 我确信问题出在ajax。 Without ajax it is fine.
没有ajax,那就很好。 This is my javascript code:
这是我的JavaScript代码:
var frm = $('#register-form');
$(document).on('submit', '#register-form', function (e) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Form was submitted!');
}
});
e.preventDefault();
});
});
The form is: 形式是:
<form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/addnow/" role="form" enctype="multipart/form-data" id="register-form" novalidate="novalidate">
...
</form>
And the controller action is: 控制器的动作是:
public function actionAddnow()
{
if(Yii::$app->request->isAjax)
{
//codes here
}
}
The problem is that when I click the submit button, nothing is happening. 问题是,当我单击提交按钮时,没有任何反应。
I see that you are using multipart/form-data, i figure that you are sending files whitin yhe form. 我看到您正在使用multipart / form-data,我认为您正在通过表单发送文件。
In that case you shuldent use: 在这种情况下,您应该避免使用:
data: frm.serialize(),
insted 插入
data: new FormData(frm),
Also you said that when you click submit nothing is hapend, but i think that you should be able to see a error in your browser console. 您还说过,当您单击“提交”时,没有任何提示,但是我认为您应该能够在浏览器控制台中看到错误。 That could be usefull info.
那可能是有用的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.