关于ajax的问题
2015-11-03 20:50
239 查看
今天 学了一点ajax 遇到了很多问题 ,现总结如下:
1 . xmlHttp.open() 里参数为true 与false的选择 true 就是异步处理 false 就是同步处理 就是当客户端请求服务时 不管服务器端是否处理完毕 就返回响应值这是异步 反之 就是同步.
2.关于我的status 值一直为0 的问题 首先 有两种可能 一种是你的网页是在本地浏览的 导致为0 还有一种就是 你的提交按钮时 submit 使得找不到了。
3. 判断state 后还要判断status 否则就会出现2的问题
4.我出现的responseText一直弹出为空就是 2 的问题
下面是我的登录完整代码
login.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>管理员登录</title>
<link rel="stylesheet" href="css/LoginCss.css" type="text/css" media="screen" charset="utf-8">
<script>
function getxmlHttpRequest()
{
var xmlHttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Mozilla, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5, IE6
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlHttp==null)
{
alert("Your browser does not support XMLHTTP.");
}
return xmlHttp;
}
var xmlHttp=getxmlHttpRequest();
function AjaxRequst()
{
var username=document.getElementById('username').value;
var password=document.getElementById('psw').value;
sendRequest('login.php',call_back,'username='+username+'&password='+password);
}
function sendRequest(url,call_back,data)
{
var data=data || "";
xmlHttp.onreadystatechange=call_back;//onreadystatechange 在发生前和发生后都会触发,要根据 readyState 判断当前状态(只有在 readyState == 4 时才是发送完)
if(data!="")
{
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length",data.length);
xmlHttp.setRequestHeader("Connection","close");
}else{
xmlHttp.open("get",url,true);
}
xmlHttp.send(data);
}
function call_back()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var res=xmlHttp.responseText;
alert(res);}
}
}
</script>
</head>
<body>
<form method="post" >
<span>管理员登录</span>
用户名:<input type="text" name="username" value="" id="username"><br/><br/>
密码:<input type="password" name="password" value="" id="psw"><br/><br/>
<input type="button" value="登录" class="button" onclick="AjaxRequst()">
</form>
</body>
</html>
login.php
<?php
header("Content-type:text/html;charset=utf-8");
if($_POST['username']=='php'&& $_POST['password']=='123')
{
echo ("hello");
}else{
echo ("用户名或密码错误");
}
?>
1 . xmlHttp.open() 里参数为true 与false的选择 true 就是异步处理 false 就是同步处理 就是当客户端请求服务时 不管服务器端是否处理完毕 就返回响应值这是异步 反之 就是同步.
2.关于我的status 值一直为0 的问题 首先 有两种可能 一种是你的网页是在本地浏览的 导致为0 还有一种就是 你的提交按钮时 submit 使得找不到了。
3. 判断state 后还要判断status 否则就会出现2的问题
4.我出现的responseText一直弹出为空就是 2 的问题
下面是我的登录完整代码
login.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>管理员登录</title>
<link rel="stylesheet" href="css/LoginCss.css" type="text/css" media="screen" charset="utf-8">
<script>
function getxmlHttpRequest()
{
var xmlHttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Mozilla, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5, IE6
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlHttp==null)
{
alert("Your browser does not support XMLHTTP.");
}
return xmlHttp;
}
var xmlHttp=getxmlHttpRequest();
function AjaxRequst()
{
var username=document.getElementById('username').value;
var password=document.getElementById('psw').value;
sendRequest('login.php',call_back,'username='+username+'&password='+password);
}
function sendRequest(url,call_back,data)
{
var data=data || "";
xmlHttp.onreadystatechange=call_back;//onreadystatechange 在发生前和发生后都会触发,要根据 readyState 判断当前状态(只有在 readyState == 4 时才是发送完)
if(data!="")
{
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length",data.length);
xmlHttp.setRequestHeader("Connection","close");
}else{
xmlHttp.open("get",url,true);
}
xmlHttp.send(data);
}
function call_back()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var res=xmlHttp.responseText;
alert(res);}
}
}
</script>
</head>
<body>
<form method="post" >
<span>管理员登录</span>
用户名:<input type="text" name="username" value="" id="username"><br/><br/>
密码:<input type="password" name="password" value="" id="psw"><br/><br/>
<input type="button" value="登录" class="button" onclick="AjaxRequst()">
</form>
</body>
</html>
login.php
<?php
header("Content-type:text/html;charset=utf-8");
if($_POST['username']=='php'&& $_POST['password']=='123')
{
echo ("hello");
}else{
echo ("用户名或密码错误");
}
?>
相关文章推荐
- 采集异步加载内容
- QDbus嵌入式环境变量
- 加载图片的工具类
- 冒泡排序-交换排序
- zoj 3537 凸包+三角剖分dp(切蛋糕)
- 残差
- 一切都是为了实现-大道至简第六章读后感
- JAVA初级试题16道,附赠答案!
- Linux基本命令(四)——文件管理
- c++中wstring、string、_bstr_t互转
- 【noip2007】树网的核
- ListView的BaseAdapter中getView方法模板代码
- kb3035583
- 回车与换行的区别
- JSP内置对象之session application page pageContext config exception
- hadoop2提交到Yarn: Mapreduce执行过程reduce分析3
- web进阶之jQuery操作DOM元素&&MySQL记录操作&&PHP面向对象学习笔记
- 顶部固定显示;某模块的标题栏在滚动时固定显示;到底部时显示底部banner;回到顶部。4个效果
- Android 中使用Fragment所引发的
- hadoop2提交到Yarn: Mapreduce执行过程分析2