今天遇到一个坑,就是利用ajax提交表单的时候,若提交成功进行跳转,一直跳转不成功。代码如下。
<div class="form-group no-margin">
<button class="btn btn-primary btn-block" onclick="register()">//这里是重点,这里需要吧type=submit这个属性去掉。可能原因是submit和form的action绑定了,所以一直跳转不成功。
Register
</button>
</div>
//Ajax部分
$.ajax({
type: 'post',
dataType: 'json',
url: 'http://localhost:8080/AjaxTest/register',
data: $('#register-form').serialize(),
async: false,
success: function (data) {
if (data.name==='200') {
alert(data.password);
window.location.href='http://localhost:8080/AjaxTest/index.html';//跳转地址
} else {
alert(data.password)
}
},
error: function () {
alert("服务器错误")
}
})