javascript - JQuery Ajax 强制页面刷新

标签 javascript jquery ajax html refresh

我这里有一个接受用户凭据的表单。如果用户具有无效的用户凭据,则该页面将不会加载,但是如果他或她具有有效的凭据,我希望该页面进行整页刷新我将如何实现?我试过添加。

window.location.href = window.location.href; 

在响应 html 上但它不起作用,似乎 jquery 正在删除脚本代码?

这是用于表单提交的 AJAX。

$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //success
        success : function(data) {
            alert(data);
            $('#loginModal').html($(data).find('#loginModal').html());
        }
    });
})

如果用户有有效的凭据,成功的响应将是这样的

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
    window.location.href = window.location.href;

</script>
</head>

但是页面没有重新加载。

最佳答案

在 success 函数中你可以使用 JavaScript 方式重新加载:

location.reload();

有点像

$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //login success
        success : function(data) {
            //... your other code
            location.reload(); //reload the page on the success
        }
    });
})

这意味着您的查询会在登录失败时抛出一个错误,因此它不会进入您的成功函数。

关于javascript - JQuery Ajax 强制页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14668481/

相关文章:

php - Jquery ajax setTimeout 表单成功后不重定向?

javascript - 获取与特定 UID 关联的所有 Firebase 数据

javascript - 这个最终数字是从哪里来的?

javascript - 如何使用 ExtJs 4 和 MVC 从另一个类访问一个类?

javascript - Jade 中 Angular.JS 中 $scope 的问题

javascript - 如何从异步调用返回响应?

javascript - 如何用jQuery写JSON?

javascript - 点击模态上的 "Contact Us"按钮时如何滚动联系我们?

jquery - 导轨 3 : how to add css to jquery for autocomplete search

javascript - jQuery .load() html 页面,里面有 javascript