asp.net ajax post方式

來源:互聯網
上載者:User
////postdemo.aspx頁面代碼<%@ Page Language="C#" AutoEventWireup="true" CodeFile="postdemo.aspx.cs" Inherits="postdemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>POST方法示範</title>    <script type="text/javascript" language="javascript">//聲明一個XMLHTTPRequest對象var request;//建立XMLHTTPRequest對象function createrequest(){if(window.ActiveXObject) //兩種都是IE瀏覽器{try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{return new ActiveXObject("Mircosoft.XMLHTTP");}catch(e1){return null;}}}else if(window.XMLHTTPRequest){return new XMLHTTPRequest();}else{return null;}}function sendMsg(){request=createrequest();if(request){request.onreadystatechange=receive;request.open("post","xml.aspx",true);//para1 提交方法 //para2 需要提交到的url 以url形式傳值 //para3 是否同步 true為同步request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');//post方法必須要設定此參數request.send(null);//使用值null調用send().因為已經在請求 URL 中添加了要發送給伺服器的資料(val),所以請求中不需要發送任何資料.}}function receive(){if(request.readyState==4)//5種狀態 0代表未初始化 1表示正在載入 2表示載入完畢 3表示互動 4表示完成{if(request.status==200)//200表示資訊成功返回 404代表提交的url錯誤{//document.getElementById("txtTime").value=request.responseText;var xml=request.responseXML;var parentNode=xml.documentElement;//得到根節點personsvar entity=xml.getElementsByTagName("person");//得到根節點下的子點person型的--數組--alert(entity.length);var str="<table border='1'>";for(var i=0;i<entity.length;i++){     var a=entity[i].childNodes[0].firstChild.nodeValue;     var b=entity[i].childNodes[1].firstChild.nodeValue;     alert(a);     str+="<tr><td>"+a+"</td><td>"+b+"</td></tr>";}str+="</table>";document.getElementById("result").innerHTML=str;}}}</script></head><body>    <form id="form1" runat="server">        <asp:TextBox ID="txtTime" runat="server" Width="350"></asp:TextBox>        <input id="btnTime" type="button"  value="Get XML Context" onclick="sendMsg()" />       <div id="result"></div>    </form></body></html>////////////////////////////////////////xm.aspx頁面後台代碼using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Xml;public partial class xml : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            Response.ContentType = "text/xml";//佈建要求類型為text/xml類型            Response.Expires = -1;            Response.Cache.SetCacheability(HttpCacheability.NoCache);            XmlTextWriter  xml = new XmlTextWriter(Response.Output);            xml.WriteStartDocument();                xml.WriteStartElement("persons");                xml.WriteStartElement("person");                    xml.WriteStartElement("name");                        xml.WriteString("張三");                    xml.WriteEndElement();                    xml.WriteStartElement("age");                        xml.WriteString("18");                    xml.WriteEndElement();                xml.WriteEndElement();                xml.WriteStartElement("person");                    xml.WriteStartElement("name");                         xml.WriteString("趙六");                    xml.WriteEndElement();                    xml.WriteStartElement("age");                        xml.WriteString("28");                    xml.WriteEndElement();                xml.WriteEndElement();                xml.WriteEndElement();            xml.WriteEndDocument();            xml.Flush();            Response.End();                  }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.