createXmlHttpRequest()函数后少了个return xmlHttp;
编码我觉得还是用encodeURIComponent的好,var queryString="n="+n+"&p="+p; return encodeURI(encodeURI(queryString)); 这两句变为
return "n="+encodeURIComponent(n)+"&p="+encodeURIComponent(p)就行了
xmlHttp.onreadystatechange=handlestatechange;就是这样的,我一般不这么写.
var xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
fg = xmlHttp.responseText;
if(parseInt(fg,10)==1){
alert(suc_msg);
}
else{alert(fal_msg);}
}
}
else{alert("正在删除,请稍候");}
}
xmlHttp.open("get", url, true);
xmlHttp.send(null);
编码我觉得还是用encodeURIComponent的好,var queryString="n="+n+"&p="+p; return encodeURI(encodeURI(queryString)); 这两句变为
return "n="+encodeURIComponent(n)+"&p="+encodeURIComponent(p)就行了
xmlHttp.onreadystatechange=handlestatechange;就是这样的,我一般不这么写.
var xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
fg = xmlHttp.responseText;
if(parseInt(fg,10)==1){
alert(suc_msg);
}
else{alert(fal_msg);}
}
}
else{alert("正在删除,请稍候");}
}
xmlHttp.open("get", url, true);
xmlHttp.send(null);
如果要加传递参数,可以做个闭包
<body>
</body>
<script>
(function(arg){ document.body.onload=function(){inject(arg);};})("a")function inject(a){alert(a);}
</script>
xmlHttp.onreadystatechange=handlestatechange,把函数赋给xmlHttp.onreadystatechange,xmlHttp.onreadystatechange用的是handlestatechange函数
xmlHttp.onreadystatechange=handlestatechange()指,把handlestatechange函数的运算结果给xmlHttp.onreadystatechange
不是一个概念
如果要传递参数,就得像3楼一样用闭包
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%
if(Request.HttpMethood =="POST")
Response.Write("POST:"+Request["n"]+",you p is "+Request["p"]);
else (Request.HttpMethood =="GET")
Response.Write("GET:"+Request["n"]+",you p is "+Request["p"]);
%>
<%
if(Request.HttpMethood =="POST")
{
Response.Clear();
Response.Write("POST:"+Request["n"]+",you p is "+Request["p"]);
Response.End();
}
else (Request.HttpMethood =="GET")
{
Response.Clear();
Response.Write("GET:"+Request["n"]+",you p is "+Request["p"]);
Response.End();
}
%>
else (...)
没见过这种语法
突然发现else(……)……这个有点汗。。
js中的:
function creatQueryString(){
var n=document.getElementById("n").value;
var p=document.getElementById("p").value;
var queryString="n="+n+"&p="+p;
return encodeURI(encodeURI(queryString));
}
function createXmlHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XmlHttp");
else if (window.XmlHttpRequest)
xmlHttp=new XmlHttpRequest();
} function handlestatechange(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var responseDiv=document.getElementById("serverResponse");
responseDiv.innerHTML=decodeURI(xmlHttp.responseText); }
}
function getp(){ createXmlHttpRequest();
var queryString="1.aspx?";
queryString +=creatQueryString()+"&time="+new Date().getTime();
xmlHttp.onreadystatechange=handlestatechange;
xmlHttp.open("GET",queryString);
xmlHttp.send(null); }
这个是html中的 <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%
if(Request.HttpMethood =="POST")
Response.Write("POST:"+Request["n"]+",you p is "+Request["p"]);
else if(Request.HttpMethood =="GET")
Response.Write("GET:"+Request["n"]+",you p is "+Request["p"]);
%>
这个是1.aspx中的运行后 并没有在 id为serverResponse的层中显示 而去浏览器也没报错!
var p=document.getElementById("p").value; 没有给n和p获取到值
Response.Write("POST:"+Request["n"]+",you p is "+Request["p"]);
else if(Request.HttpMethood =="GET")
Response.Write("GET:"+Request["n"]+",you p is "+Request["p"]);这个执行了没?
如果没有值,就应该是服务器方的问题了
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XmlHttp"); 改为
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 试试
最好是放到函数外面执行<script language="javascript">
<body>
<input id="n"><input id="p">
<div id="1"></div>
</body>
<script language="javascript">
var xmlHttp=false;
try{
xmlHttp=new XmlHttpRequest();
}catch(tryMicrosoftIE){
try{
xmlHttp=new ActiveXObject("msxml2.XMLHTTP");
}catch(tryOtherMicrosoftIE){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(failed){
xmlHttp=false;
}
}
}
if(xmlHttp) alert("xmlhttprequest对像初始化失败");
function creatQueryString(){
var n=document.getElementById("n").value;
var p=document.getElementById("p").value;
var queryString="n="+n+"&p="+p;
return queryString;
} function handlestatechange(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var responseDiv=document.getElementById("1");
responseDiv.innerHTML=decodeURI(xmlHttp.responseText);
}
}
function getp(){
//createXmlHttpRequest();
var queryString="1.html?";
queryString +=creatQueryString()+"&time="+new Date().getTime();
xmlHttp.onreadystatechange=handlestatechange;
xmlHttp.open("GET",queryString,true);
xmlHttp.send(null);
} getp();
</script> </script>