use*_*206 3 html javascript ajax jquery refresh
我在这里有一个接受用户凭据的表单。如果用户的用户凭据无效,页面将不会加载,但是如果他或她拥有有效的凭据,我希望页面刷新整页我将如何实现?我试过添加。
window.location.href = window.location.href;
Run Code Online (Sandbox Code Playgroud)
在响应 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());
}
});
})
Run Code Online (Sandbox Code Playgroud)
如果用户具有有效凭据,则成功的响应将是这个
<!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>
Run Code Online (Sandbox Code Playgroud)
但页面没有重新加载。
在成功函数中,您可以使用 JavaScript 方式重新加载:
location.reload();
Run Code Online (Sandbox Code Playgroud)
有点像这样
$('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
}
});
})
Run Code Online (Sandbox Code Playgroud)
这意味着您的查询在登录失败时会引发错误,因此它不会出现在您的成功功能中。