<p style="text-indent: 10.5pt;">Ajax<span style="">的</span>GET<span style="">和</span>POST<span style="">的两种提交方法</span>,get<span style="">主要是少量数据的提交</span>,<span style="">在这里我谈谈用</span>post<span style="">方法发送大量数据的过程</span>.</p>
<p style="text-indent: 10.5pt;"><span style="">此处我将所有内容打包为一个</span>xml<span style="">文件流</span>,<span style="">避免了长参数的发送</span>,<span style="">在服务器端直接载入</span>xml<span style="">解析</span>,<span style="">十分方便</span>,<span style="">数据也没有丢失现象</span>.</p>
<p style="text-indent: 10.5pt;"><span style="">下为过程的实现思路</span>:</p>
<p style="text-indent: 10.5pt;"><span style="">第一步</span>:<span style="">创建一个</span>XML<span style="">的</span>DOM<span style="">对象</span>,<span style="">可用如下方法完成</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">function CreateDomDoc() </span><span style="color: black;">//</span><span style="">创建</span><span style="color: black;">XML</span><span style="">文档对象</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">{</span></p>
<p align="left" style=""><span style="color: red;">Var signatures = ["Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","Msxml2.DOMDocument.3.0","Msxml2.DOMDocument","Microsoft.XmlDom"];</span></p>
<p><span style="color: red;"> for(var i=0;i<signatures.length;i++)</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> try</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> var domDoc = new ActiveXObject(signatures[i]);</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> return domDoc;</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> catch(e)</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> return null;</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">}</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"></span></p>
<p><span style="">第二步</span><span style="color: black;">:</span><span style="">从客户端取得数据写入</span><span style="color: black;">XM,</span><span style="">方法如下</span><span style="color: black;">:</span></p>
<p><span style="color: red;">function CreateXml(doc)</span></p>
<p><span style="color: red;">{</span></p>
<p style="text-indent: 15.75pt;"><span style="color: red;">var root= doc.createElement("root");</span></p>
<p><span style="color: red;"> </span></p>
<p><span style="color: red;"> var title= doc.createElement("title");</span></p>
<p><span style="color: red;"> title.text= document.getElementById("title").value;</span></p>
<p><span style="color: red;"> root.appendChild(title);</span></p>
<p><span style="color: red;"> var homepage = doc.createElement("homepage");</span></p>
<p><span style="color: red;"> homepage.text= document.getElementById("homepage").value;</span></p>
<p><span style="color: red;"> root.appendChild(homepage);</span></p>
<p><span style="color: red;"> doc.appendChild(root);</span></p>
<p><span style="color: red;"> //alert("ddddd");</span></p>
<p><span style="color: red;"> return doc.xml;</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="">第三步</span><span style="color: black;"></span><span style="">互相调用</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">var domDoc = CreateDomDoc();</span><span style="color: black;">//</span><span style="">创建对象</span><span style="color: black;"></span><span style="">将用</span><span style="color: black;">send</span><span style="">发送到服务器端</span></p>
<p><span style="color: red;">if(domDoc!=null)</span></p>
<p><span style="color: red;">{</span></p>
<p><span style="color: red;"> var xml = CreateXml(domDoc);</span><span style="color: black;">//</span><span style="">写入</span><span style="color: black;">xml </span><span style="">返回</span><span style="color: black;">xml</span><span style="">文档</span></p>
<p><span style="color: red;"> alert(domDoc);//</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="color: red;">else</span></p>
<p><span style="color: red;">{</span></p>
<p><span style="color: red;"> alert("</span><span style="">未安装</span><span style="color: red;">MSXML</span><span style="">控件</span><span style="color: red;">");</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="">第四步</span><span style="color: black;">AJAX</span><span style="">发送</span></p>
<p><span style="color: red;">var xmlhttp=createxmlhttp(); </span></p>
<p><span style="color: red;"></span></p>
<p><span style="color: red;"> xmlhttp.onreadystatechange = function()</span></p>
<p><span style="color: red;"> {</span></p>
<p><span style="color: red;"> if(xmlhttp.readyState == 4)</span></p>
<p><span style="color: red;"> {</span></p>
<p><span style="color: red;"> if(xmlhttp.status == 200)</span></p>
<p><span style="color: red;"> { </span></p>
<p><span style="color: red;"> </span></p>
<p><span style="color: red;"> info =xmlhttp.responseText;</span></p>
<p><span style="color: red;"> //alert(info);</span></p>
<p><span style="color: red;"> }</span></p>
<p><span style="color: red;"> else</span></p>
<p><span style="color: red;"> { </span></p>
<p><span style="color: red;"> alert("ajax</span><span style="">调用失败!</span><span style="color: red;">"+xmlhttp.status);</span></p>
<p><span style="color: red;"> } </span></p>
<p><span style="color: red;"> } </span></p>
<p><span style="color: red;"> }</span></p>
<p><span style="color: red;">xmlhttp.open("POST","ajaxserver.aspx?type=add",false);</span></p>
<p><span style="color: red;">xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); </span><span style="color: black;">//</span><span style="">关键</span><span style="color: black;">,</span><span style="">否则出错</span></p>
<p><span style="color: red;">xmlhttp.send(domDoc);</span><span style="color: black;">//</span><span style="">发送一定是</span><span style="color: black;">domDoc</span><span style="">文档对象</span><span style="color: black;">,</span><span style="">如果只发送</span><span style="color: black;">xml</span><span style="">将出错</span><span style="color: black;">”statue=500” </span></p>
<p><strong><span style="">服务器端</span></strong><strong><span style="color: black;">:</span></strong></p>
<p><span style="">设置接受类型</span><span style="color: black;">,</span><span style="">防止中文出现乱码情况</span></p>
<p><span style="">Request.ContentEncoding = System.Text.<span style="color: teal;">Encoding</span>.GetEncoding(<span style="color: maroon;">"gb2312"</span>);</span></p>
<p align="left" style="text-align: left;"><span style="">XmlDocument xmldoc = new XmlDocument(); </span><span style="">//</span><span style="">建立xml文档对象</span></p>
<p align="left" style="text-align: left;"><span style="">xmldoc.Load(Request.InputStream); </span><span style="">//</span><span style="">接受ajax发送的xml文档对象流//也可以接受普通字符流</span></p>
<p align="left" style="text-align: left;"><span style="">XmlNode node = xmldoc.SelectSingleNode("//title"); </span><span style="">//</span><span style="">获得title节点</span></p>
<p><span style="">title = node.InnerText;</span><span style=""> //</span><span style="">取得节点值</span></p>
<p><strong><span style="">部分解释:</span></strong></p>
<p><span style="color: red;">xmlhttp.open("POST","ajaxserver.aspx?type=add",false);</span></p>
<p><span style="color: black;">open</span><span style="">方法至少包含上述三个参数</span><span style="color: black;">,</span></p>
<p><span style="color: black;">post/get:</span><span style="">发送类型</span></p>
<p><span style="color: black;">ajaxserver.aspx?type=add :</span><span style="">请求页面路径</span></p>
<p><span style="color: black;">false: </span><span style="">可以是</span><span style="color: black;">true,</span><span style="">当为</span><span style="color: black;">true</span><span style="">是请求异步</span><span style="color: black;">,</span><span style="">发送可以可以继续使用表单</span><span style="color: black;">,false</span><span style="">时发送后锁定表单</span><span style="color: black;">,</span><span style="">直到返回结果</span><span style="color: black;">.</span></p>
<p style="text-indent: 10.5pt;"><span style="">此处我将所有内容打包为一个</span>xml<span style="">文件流</span>,<span style="">避免了长参数的发送</span>,<span style="">在服务器端直接载入</span>xml<span style="">解析</span>,<span style="">十分方便</span>,<span style="">数据也没有丢失现象</span>.</p>
<p style="text-indent: 10.5pt;"><span style="">下为过程的实现思路</span>:</p>
<p style="text-indent: 10.5pt;"><span style="">第一步</span>:<span style="">创建一个</span>XML<span style="">的</span>DOM<span style="">对象</span>,<span style="">可用如下方法完成</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">function CreateDomDoc() </span><span style="color: black;">//</span><span style="">创建</span><span style="color: black;">XML</span><span style="">文档对象</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">{</span></p>
<p align="left" style=""><span style="color: red;">Var signatures = ["Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","Msxml2.DOMDocument.3.0","Msxml2.DOMDocument","Microsoft.XmlDom"];</span></p>
<p><span style="color: red;"> for(var i=0;i<signatures.length;i++)</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> try</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> var domDoc = new ActiveXObject(signatures[i]);</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> return domDoc;</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> catch(e)</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> {</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> }</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"> return null;</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">}</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;"></span></p>
<p><span style="">第二步</span><span style="color: black;">:</span><span style="">从客户端取得数据写入</span><span style="color: black;">XM,</span><span style="">方法如下</span><span style="color: black;">:</span></p>
<p><span style="color: red;">function CreateXml(doc)</span></p>
<p><span style="color: red;">{</span></p>
<p style="text-indent: 15.75pt;"><span style="color: red;">var root= doc.createElement("root");</span></p>
<p><span style="color: red;"> </span></p>
<p><span style="color: red;"> var title= doc.createElement("title");</span></p>
<p><span style="color: red;"> title.text= document.getElementById("title").value;</span></p>
<p><span style="color: red;"> root.appendChild(title);</span></p>
<p><span style="color: red;"> var homepage = doc.createElement("homepage");</span></p>
<p><span style="color: red;"> homepage.text= document.getElementById("homepage").value;</span></p>
<p><span style="color: red;"> root.appendChild(homepage);</span></p>
<p><span style="color: red;"> doc.appendChild(root);</span></p>
<p><span style="color: red;"> //alert("ddddd");</span></p>
<p><span style="color: red;"> return doc.xml;</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="">第三步</span><span style="color: black;"></span><span style="">互相调用</span></p>
<p style="text-indent: 10.5pt;"><span style="color: red;">var domDoc = CreateDomDoc();</span><span style="color: black;">//</span><span style="">创建对象</span><span style="color: black;"></span><span style="">将用</span><span style="color: black;">send</span><span style="">发送到服务器端</span></p>
<p><span style="color: red;">if(domDoc!=null)</span></p>
<p><span style="color: red;">{</span></p>
<p><span style="color: red;"> var xml = CreateXml(domDoc);</span><span style="color: black;">//</span><span style="">写入</span><span style="color: black;">xml </span><span style="">返回</span><span style="color: black;">xml</span><span style="">文档</span></p>
<p><span style="color: red;"> alert(domDoc);//</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="color: red;">else</span></p>
<p><span style="color: red;">{</span></p>
<p><span style="color: red;"> alert("</span><span style="">未安装</span><span style="color: red;">MSXML</span><span style="">控件</span><span style="color: red;">");</span></p>
<p><span style="color: red;">}</span></p>
<p><span style="">第四步</span><span style="color: black;">AJAX</span><span style="">发送</span></p>
<p><span style="color: red;">var xmlhttp=createxmlhttp(); </span></p>
<p><span style="color: red;"></span></p>
<p><span style="color: red;"> xmlhttp.onreadystatechange = function()</span></p>
<p><span style="color: red;"> {</span></p>
<p><span style="color: red;"> if(xmlhttp.readyState == 4)</span></p>
<p><span style="color: red;"> {</span></p>
<p><span style="color: red;"> if(xmlhttp.status == 200)</span></p>
<p><span style="color: red;"> { </span></p>
<p><span style="color: red;"> </span></p>
<p><span style="color: red;"> info =xmlhttp.responseText;</span></p>
<p><span style="color: red;"> //alert(info);</span></p>
<p><span style="color: red;"> }</span></p>
<p><span style="color: red;"> else</span></p>
<p><span style="color: red;"> { </span></p>
<p><span style="color: red;"> alert("ajax</span><span style="">调用失败!</span><span style="color: red;">"+xmlhttp.status);</span></p>
<p><span style="color: red;"> } </span></p>
<p><span style="color: red;"> } </span></p>
<p><span style="color: red;"> }</span></p>
<p><span style="color: red;">xmlhttp.open("POST","ajaxserver.aspx?type=add",false);</span></p>
<p><span style="color: red;">xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); </span><span style="color: black;">//</span><span style="">关键</span><span style="color: black;">,</span><span style="">否则出错</span></p>
<p><span style="color: red;">xmlhttp.send(domDoc);</span><span style="color: black;">//</span><span style="">发送一定是</span><span style="color: black;">domDoc</span><span style="">文档对象</span><span style="color: black;">,</span><span style="">如果只发送</span><span style="color: black;">xml</span><span style="">将出错</span><span style="color: black;">”statue=500” </span></p>
<p><strong><span style="">服务器端</span></strong><strong><span style="color: black;">:</span></strong></p>
<p><span style="">设置接受类型</span><span style="color: black;">,</span><span style="">防止中文出现乱码情况</span></p>
<p><span style="">Request.ContentEncoding = System.Text.<span style="color: teal;">Encoding</span>.GetEncoding(<span style="color: maroon;">"gb2312"</span>);</span></p>
<p align="left" style="text-align: left;"><span style="">XmlDocument xmldoc = new XmlDocument(); </span><span style="">//</span><span style="">建立xml文档对象</span></p>
<p align="left" style="text-align: left;"><span style="">xmldoc.Load(Request.InputStream); </span><span style="">//</span><span style="">接受ajax发送的xml文档对象流//也可以接受普通字符流</span></p>
<p align="left" style="text-align: left;"><span style="">XmlNode node = xmldoc.SelectSingleNode("//title"); </span><span style="">//</span><span style="">获得title节点</span></p>
<p><span style="">title = node.InnerText;</span><span style=""> //</span><span style="">取得节点值</span></p>
<p><strong><span style="">部分解释:</span></strong></p>
<p><span style="color: red;">xmlhttp.open("POST","ajaxserver.aspx?type=add",false);</span></p>
<p><span style="color: black;">open</span><span style="">方法至少包含上述三个参数</span><span style="color: black;">,</span></p>
<p><span style="color: black;">post/get:</span><span style="">发送类型</span></p>
<p><span style="color: black;">ajaxserver.aspx?type=add :</span><span style="">请求页面路径</span></p>
<p><span style="color: black;">false: </span><span style="">可以是</span><span style="color: black;">true,</span><span style="">当为</span><span style="color: black;">true</span><span style="">是请求异步</span><span style="color: black;">,</span><span style="">发送可以可以继续使用表单</span><span style="color: black;">,false</span><span style="">时发送后锁定表单</span><span style="color: black;">,</span><span style="">直到返回结果</span><span style="color: black;">.</span></p>