<mce:script type="text/javascript"><!-- var xmlhttp; function Button1_onclick() { var dzb = "info={'FullName':'王继军','Company':'上海天正','Address':'田林路388号'}"; //"Departments":["总经理室","酒店事业部","市场销售部","金融事业部"] send(dzb); } function send(arg) { CreateXMLHttpRequest(); xmlhttp.onreadystatechange = callhandle; //xmlhttp.open("GET","Default.aspx?goback=yes&arg=" + arg,true); xmlhttp.open("POST","ajaxpost2.aspx?goback=yes",true); xmlhttp.setRequestHeader("Content-Length",arg.lenght); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //用POST的时候一定要有这句 xmlhttp.send(arg); } function CreateXMLHttpRequest() { if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } } function callhandle() { if (xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { var dzb = eval("(" + xmlhttp.responseText +")"); alert(dzb.Address); } } } // --></mce:script>
if (Request.QueryString["goback"].ToString() == "yes") { Response.Write(Request.Form["info"].ToString()); Response.Flush(); Response.Close(); }
注意:在HTML代码中一定要有xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //用POST的时候一定要有这句,用GET的时候可以不用这句
我再补充一点,用XMLHttpRequest想后台发送的时候,是没有什么Page.IsPostBack的,即他总是认为是第一次加载。