【发布时间】:2011-07-20 05:43:47
【问题描述】:
我最初有一个这样设置的表单(CSS 样式已被删除)
<form name="LoginForm" action="login.php" method="post">
<input name="email" id="email" type="text"></input>
<input name="password" id="password" type="password"></input>
<input name="login" id="login" type="submit" value="Login"></input>
</form>
它运行良好,login.php 将验证用户信用。但是,这种方法需要页面重定向。我正在尝试将代码迁移到 AJAX,以便我可以查询登录详细信息并留在页面内。 [编辑] 这是我使用的 AJAX 对象
function Ajax(){
this.xmlhttp=null; //code below will assign correct request object
if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari
this.xmlhttp=new XMLHttpRequest();
}
else{ // code for IE6, IE5
this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
this.stateChangeFunction=function(){}; //user must reimplement this
var that=this;
this.xmlhttp.onreadystatechange=function(){ //executes the appropriate code when the ready state and status are correct
if (this.readyState==4 && this.status==200){
that.stateChangeFunction();
}
else{
dump("Error");
}
}
}
然后我有一个 login.js 函数,我不太清楚如何合并,目前我将它添加到提交按钮的 onclick 事件中:
function login(email,password){
var ajax=new Ajax();
//ajax.xmlhttp.open("GET","login.php?LoginEmailField="+email+",LoginPasswordField="+password,true);
//ajax.xmlhttp.send();
}
您会注意到最后两行是如何被注释掉的,我现在不太确定如何发送参数,但关键是即使这两行被注释掉,整个站点仍然会重新加载。在表单中使用 AJAX 的正确方法是什么。
谢谢
【问题讨论】: