Javascript 输入错误的用户名/密码而不显示错误消息时刷新页面
我正在使用ajax提交loginform并检查凭据是否正确。当我输入正确的用户名和密码时,它将成功登录,但当我输入错误的用户名/密码时,页面将刷新而不显示任何消息。我无法确定背后的原因。任何帮助都将不胜感激。提前感谢Javascript 输入错误的用户名/密码而不显示错误消息时刷新页面,javascript,html,ajax,Javascript,Html,Ajax,我正在使用ajax提交loginform并检查凭据是否正确。当我输入正确的用户名和密码时,它将成功登录,但当我输入错误的用户名/密码时,页面将刷新而不显示任何消息。我无法确定背后的原因。任何帮助都将不胜感激。提前感谢 <form action="" method="post" id="login-form"> <div class="login_fields"> <label>Email</label> <input
<form action="" method="post" id="login-form">
<div class="login_fields">
<label>Email</label>
<input name="your_email" id="user-name" type="email" class="required textfield" autofocus required>
<label>Password</label>
<input name="your_password" id="password" type="password" class="required textfield" required>
<div class="login_buttons">
<button type="submit" class="contact-send contact-button" >Login</button>
<a class="register-button">Register</a>
</div>
</div>
</form>
<script>
$(document).ready(function()
{
$(".contact-send").click(function()
{
$.ajax({
url: "/index/loginform",
data: $("#login-form").serialize() + "&action=send",
type: "post",
cache: false,
dataType: "html",
success : function(resp)
{
if(resp=="send-1")
{
alert("invalid username/password");
}
},
error : function(error,st,e)
{
console.log(error,st,e);
}
});
});
});
</script>
电子邮件
密码
登录
登记
$(文档).ready(函数()
{
$(“.contact send”)。单击(函数()
{
$.ajax({
url:“/index/loginform”,
数据:$(“#登录表单”).serialize()+“&action=send”,
类型:“post”,
cache:false,
数据类型:“html”,
成功:功能(resp)
{
如果(resp==“发送-1”)
{
警报(“无效用户名/密码”);
}
},
错误:函数(错误,st,e)
{
控制台日志(错误,st,e);
}
});
});
});
当您“警告”错误时,它只会将其转换为字符串,即[object object]
,请尝试控制台。记录错误。警告您从ajax页面得到的响应数据。尝试在console.log中打印以进行检查。它将在控制台中打印对象Add return false
success : function(resp)
{
if(resp=="send-1"){
alert("invalid username/password"););
return false;
}
},
error : function(error,st,e)
{
console.log(error,st,e);
return false;
}
此路径“/../loginform”将导致一些问题…我已将url设置为“/index/loginform”,这是有效的路径吗?web服务器帐户是否有权限访问那里的文件?是的,有权限。如果凭据正确,它将成功登录,但如果给定的凭据不正确,则不会显示任何错误。可能是(错误)使用了保留字?('data')我尝试使用console.log(error+st+e)。我得到了相同的错误。@ChetanGawai使用逗号。。。。使用+
只能转换为字符串<代码>控制台日志(错误,st,e)代码>页面正在刷新。我无法看到什么控制台。日志(error,st,r)打印正确。如果页面正在刷新,则看起来您确实进入了ajax成功功能,因为这是您告诉页面刷新的地方。在执行ajax请求时不应进行刷新。在提交表单时,您可能还希望避免使用default(),这样它就不会执行默认提交。Donnywals:没有位置。reload()页面也会刷新。嘿,Chetan,在这种情况下,请检查您的AJAX页面,尝试直接访问而不是AJAX调用(仅用于测试)并在控制台中打印数据。查看是否获得所需的输出,然后尝试AJAX调用模式由于按钮类型为submit button,如果未强制表单不提交,则默认情况下表单将被提交。。或者您可以将按钮类型更改为“button”Roy MJ:我已根据您的建议对代码进行了更改,但问题仍然存在。嗨,Roy MJ:谢谢您的建议。它是在堆栈溢出时编辑代码时错误留下的。我的主代码有正确的括号。