asp.net 版本 Ajax post xml

 我一個朋友在看AJAX基礎教程,因那裏面的源碼是JAVA版本的,所以他在將其中一個示例改成asp.net的時候出現了 status 500的錯誤.然後問我怎麼改,我試了一下,下面貼出這個示例的代碼.  ,其實是asp.net在提交FORM的時候會檢測危險元素....

不多說了,直接上代碼:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="3-2.aspx.cs" Inherits="_3_2" %>
  2. <html>
  3. <head runat="server">
  4.     <title>Sending an xml Request</title>
  5.     <script type="text/javascript">
  6.         var xmlHttp;
  7.         
  8.         function createXMLHttpRequest()
  9.         {
  10.             if (window.ActiveXObject){
  11.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  12.             }
  13.             else if (window.XMLHttpRequest){
  14.                 xmlHttp=new XMLHttpRequest();
  15.             }
  16.         }
  17.         
  18.         function CreateDomDoc() //創建XML文檔對象
  19.         {
  20.         var signatures = ["Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","Msxml2.DOMDocument.3.0","Msxml2.DOMDocument","Microsoft.XmlDom"];
  21.         for(var i=0;i<signatures.length;i++)
  22.         {
  23.           try
  24.           {
  25.             var domDoc = new ActiveXObject(signatures[i]);
  26.             return domDoc;
  27.           }
  28.           catch(e)
  29.           {
  30.                 alert("您的瀏覽器不支持xml組件");
  31.           }
  32.         }
  33.         return null;
  34.         }
  35.         
  36.         function createXML()
  37.         {
  38. //            var xml="<pets>";
  39.             var options=document.getElementById("petTypes").childNodes;
  40.             var option=null;
  41. //            for(var i=0;i<options.length;i++)
  42. //            {
  43. //                option=options[i];
  44. //                if (option.selected)
  45. //                {
  46. //                    xmlxml=xml+"<type>"+option.value+"<//type>";
  47. //                }
  48. //            }
  49. //            
  50. //            xmlxml=xml+"<//pets>";
  51. //            return xml;
  52.             var doc=CreateDomDoc();
  53.             var root=doc.createElement("pets");
  54.             var type;
  55.             
  56.             for (var i=0;i<options.length;i++)
  57.             {
  58.                 option=options[i];
  59.                 
  60.                 if (option.selected)    
  61.                 {
  62.                     type=doc.createElement("type");
  63.                     type.text=option.value;
  64.                     root.appendChild(type);        
  65.                 }
  66.             }
  67.             
  68.             doc.appendChild(root);
  69.             return doc;
  70.             
  71.             var type
  72.         }
  73.         
  74.         function sendPetTypes(){
  75.             createXMLHttpRequest();
  76.             
  77.             var xml=createXML();  // 注意此處要傳xmlDoc的對象,要不會出現Status 500的錯誤
  78.             var url="3-2.aspx";
  79.             
  80.             xmlHttp.open("POST",url,true);
  81.             xmlHttp.onreadystatechange=handleStateChange;
  82.             xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
  83.             xmlHttp.send(xml);
  84.             //alert(xml);
  85.         }
  86.         
  87.         function handleStateChange(){
  88.          //alert(xmlHttp.readyState);
  89.             if (xmlHttp.readyState==4){
  90.            // alert(xmlHttp.status);
  91.                 if (xmlHttp.status==200)
  92.                 {
  93.                     parseResults();
  94.                    // alert(xmlHttp.responseText);
  95.                 }
  96.             }
  97.         }
  98.         
  99.         function parseResults()
  100.         {
  101.             var responseDiv=document.getElementById("serverResponse");
  102.             
  103.             if (responseDiv.hasChildNodes())
  104.             {
  105.                 responseDiv.removeChild(responseDiv.childNodes[0]);
  106.             }
  107.             
  108.             var responseText=document.createTextNode(xmlHttp.responseText);
  109.             responseDiv.appendChild(responseText);
  110.         }
  111.     </script>
  112. </head>
  113. <body>
  114. <h1>Select the types of pets in your home:</h1>
  115.     <form id="form1" runat="server">
  116.     
  117.         <select id="petTypes" size="6" multiplemultiple=multiple>
  118.             <option value="cats">Cats</option>
  119.             <option value="dogs">Dogs</option>
  120.             <option value="fish">Fish</option>
  121.             <option value="birds">Birds</option>
  122.             <option value="hamsters">Hamsters</option>
  123.         </select>
  124.         <br /><br />
  125.         <input type=button value="Submit Pets" onclick="sendPetTypes();" />
  126.     </form>
  127.     <h2>Server Response:</h2>
  128.     <div id="serverResponse"></div>
  129. </body>
  130. </html>

後臺 3-2.aspx.cs

 

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Xml;
  12. public partial class _3_2 : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         //System.IO.StreamReader str = Request.InputStream;
  17.         if (Request.InputStream.Length > 0)
  18.         {
  19.             XmlDocument xmlDoc = new XmlDocument();
  20.             try
  21.             {
  22.                 xmlDoc.Load(Request.InputStream);
  23.             }
  24.             catch 
  25.             {
  26.                 System.IO.Stream inStream = Request.InputStream;
  27.                 byte[] buffer = new byte[inStream.Length];
  28.                 inStream.Read(buffer, 0, buffer.Length);
  29.                 string szXML = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
  30.                 xmlDoc.LoadXml(szXML);
  31.             }
  32.             string responseText = "You have selected:";
  33.             XmlNodeList nodelist = xmlDoc.GetElementsByTagName("type");
  34.             if (xmlDoc.GetElementsByTagName("type").Count>0)
  35.             {
  36.                 for (int i = 0; i < nodelist.Count; i++)
  37.                 {
  38.                     responseText = responseText +"/n"+ nodelist[i].ChildNodes[0].Value;
  39.                     //responseText = responseText+"/n" + nodelist[i].FirstChild.Value;
  40.                 }
  41.             }
  42.             Response.ContentType = "text/xml";
  43.             Response.Write(responseText);
  44.             Response.End();
  45.         }
  46.     }
  47. // From:飛鳥工作室   http://www.birdzone.cn

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章