首页 > web前端 > js教程 > 正文

使用Ajax技术通过XMLHttpRequest对象完成首页登录功能

亚连
发布: 2018-05-25 15:43:17
原创
1585 人浏览过

这篇文章主要介绍了使用Ajax技术通过XMLHttpRequest对象完成首页登录功能,很不错的尝试,需要的朋友可以参考下

最近使用Ajax技术通过XMLHttpRequest对象完成一个首页的登录功能!

代码如下:


<script type="text/javascript">

//创建XMLHttpRequest对象

function createXMLHttpRequest(){

if(window.XMLHttpRequest){
return xmlhttprequest=new XMLHttpRequest();
}else{
return xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP");
}

}

//登录按钮执行的方法

function doStart(){

var logname=document.getElementById("loginName").value;
var logpass=document.getElementById("loginPsw").value;

var userinfo="inAccount="+logname+"&inPsw="+logpass;

var url="users/users_pswCheck.action";

xmlhttprequest=createXMLHttpRequest();

xmlhttprequest.onreadystatechange=getresultValue;

xmlhttprequest.open("post",url,true);
xmlhttprequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttprequest.send(userinfo);

}

//回调方法

function getresultValue(){
if(xmlhttprequest.readyState==4 && xmlhttprequest.status==200){

var result=xmlhttprequest.responseText;

if(result=="success"){
window.location.href="index.jsp" rel="external nofollow" ;
} else {

document.getElementById("xiaoxi").innerHTML="登录失败!";

}

}

}

//页面的按键事件,即当按的是回车键时触发该事件

function keybutton(){

if(event.keyCode==13){ 
doStart(); 
return;
} 
}
</script>
登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

SSH+Jquery+Ajax框架整合

如何利用jQuery post传递含特殊字符的数据

ajax与传统web开发的异同点

以上是使用Ajax技术通过XMLHttpRequest对象完成首页登录功能的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!