Javascript JQuery Ajax强制页面刷新
我这里有一个接受用户凭证的表单。如果用户具有无效的用户凭据,页面将不会加载。但是,如果用户具有有效凭据,我希望页面执行完整页面刷新,我将如何实现这一点?我试着加上Javascript JQuery Ajax强制页面刷新,javascript,jquery,ajax,html,refresh,Javascript,Jquery,Ajax,Html,Refresh,我这里有一个接受用户凭证的表单。如果用户具有无效的用户凭据,页面将不会加载。但是,如果用户具有有效凭据,我希望页面执行完整页面刷新,我将如何实现这一点?我试着加上 window.location.href = window.location.href; 在响应html上,但它不起作用,jquery似乎正在删除脚本代码 这里是表单提交的AJAX $('body').on('submit', '#sign-in', function(e) { e.preventDefault();
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>
在此处插入标题
window.location.href=window.location.href;
但是页面没有重新加载。查看您是否可以在成功功能中使用此代码 收到有效响应时(如1)
在成功函数中,您可以使用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
}
});
})
这意味着您的查询在登录失败时出错,因此不会进入成功功能。但是如果用户输入无效,页面将重新加载。仅当用户插入了有效凭据时才希望重新加载页面这就是为什么我在成功功能中说即使用户插入了无效凭据,成功仍将运行。它返回不同的HTML数据。因此,无论无效或无效,成功仍将运行良好,有两种可能性。第一:登录失败时抛出异常。因此,Ajax请求回退到“error”函数。第二:返回json或任何可以解析的内容,然后签入
数据
您收到它登录成功。如果是,location.reload()
else,请告诉我发生了错误,我将在哪里插入该代码?在成功功能内部?或者在已响应的html中?在php中检查来自php的响应后,在成功函数中,您可以在每个成功登录时选择1,在每个失败登录时选择0。
$('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
}
});
})