我有一个 ajax 调用,在调用另一个 URL 后我试图重定向我的页面。但是,它只是刷新页面。我可以做些什么来重定向我的页面?
<button onclick="SignUp()" class="btn btn-success text-center">Submit Information</button>
function SignUp() {
var first_name = document.getElementById('first_name').value;
var last_name = document.getElementById('last_name').value;
$.ajax({
type: 'POST',
url: 'signup.php',
data: { "first_name": first_name, "last_name": last_name },
async: false,
success: (function () {
window.location.href = "http://stackoverflow.com";
})
});
}
最佳答案
使用
window.location.href = "http://stackoverflow.com";
编辑
因为按钮在表单中,所以它的默认行为是提交
所以你必须添加 type="button"
到 button like
<button type="button" >Submit Information</button>
或
你需要使用return false
HTML
<button onclick="return SignUp()" >Submit Information</button>
JavaScript
function SignUp() {
//Your code
return false;
};
关于javascript - 在 JavaScript/jQuery 中使用 AJAX 后重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21365008/