第一次写AJAX,高手们指点
我用html文本框,输入内容插入数据库,实现异步无刷新
在js文件夹里面.js文件里面:
window.onload=function(){
var btnAdd=document.getElementById("button");
btnAdd.onclick=function(){
addCheck();//为按钮注册事件
}
};
debugger;
function addCheck(){
var Code=document.getElementById("code").value;
var fname=document.getElementById("firstName").value;
var lname=document.getElementById("lastName").value;
var email=document.getElementById("email").value;
var url="../UIPage/AddCheckBuy.ashx?"+createString(Code,fname,lname,email)+"&ts="+new Date().getTime();
//AddCheckBuy.ashx在UIPage文件夹下,UIPage和js文件夹在同一目录下
xmlHttp=createXmlHttpRequest();
xmlHttp.onreadystatechange=handleinsert;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function handleinsert(){
if(xmlHttp.readyState==4){
}
}
debugger;
function createString(code,fname,lname,email,tel,address,address1,city,citycop,zip){
var url="code=?"+code+"&fname="+fname+"&lname="+lname+"&email="+email;
return url;
}
//创建xmlHttpRequest对象
var xmlHttp;
function createXmlHttpRequest(){
if(window.ActiveXObject)
{
var xmlHttps=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp4.0",
"MSXML2.XMLHttp3.0","MSXML2.XMLHttp","Microsoft.XMLHTTP"];
for(var i=0;i<xmlHttps.length;i++)
{
try{
var xl=new ActiveXObject(xmlHttps[i]);
return xl;
}catch(error){
}
}
}
else
{
var xl=new XMLHttpRequest();
return xl;
}
throw new Error("创建失败");
}
//在CheckBuy.ashx中
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
string code = context.Request.QueryString[0].ToString();
context.Server.Transfer("Accounts.aspx");
}
public bool IsReusable {
get {
return true;
}
}
但就是没反应,并且报context.Request.QueryString[0]对象为空对象
请问错在哪里,还有有什么更好的方法?