我有一个如下所示的 HTML 表单:
<form class="form-horizontal" id="create_user" method="POST" action="#" accept-charset="UTF-8">
<span>Username</span><input required="true" type="text" name="username" id="username">
<span>E-mail</span><input type="email" name="email" id="email">
<span>Privileges</span><select id="role" name="role"><option value="1">Administrator</option><option value="2">Employee</option><option value="3">Dealer</option></select>
<span>Password</span><input type="password" name="password" id="password">
<span>Repeat Password</span><input type="password" name="repeat_password" id="repat_password">
<input class="btn-primary btn" type="submit" value="Submit">
<input class="btn-inverse btn" type="reset" value="Reset">
</form>
我正在尝试使用 AJAX 将数据发送到服务器,如下所示:
$("form").submit(function(){
var form_data = $(this).serialize();
var request = $.ajax({
url: "user/validate",
type: "POST",
data: form_data
});
request.done(function (response, textStatus, jqXHR){
alert(response);//Comes back blank
})
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
});
编辑:这是我处理 POST 数据的 Laravel 控制器:
class User_Controller extends Base_Controller {
public function post_validate() {
return Input::get('username');
}
}
但是,不会使用 发送数据$(this).serialize()
。如果我通过执行以下操作单独传递每个元素:$("#username").val()
,它工作正常。我确保我的所有表单输入都有名称,但它仍然不起作用。