Asp.net Ajax【用戶端二】Sys.Net.WebRequest

來源:互聯網
上載者:User

先從代碼開始

用戶端請求頁面Client.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>無標題頁</title>
    <script type="text/javascript">
      function pageLoad() {
      }
      function onCompleteCallback(executor,eventArgs)
      {
        if(executor.get_responseAvailable())
        {
           var div=$get("div1");
            div.innerHTML=executor.get_responseData()+"<br/><b>"
                                        +executor.getAllResponseHeaders()+"</b>";
        }
      }
    function call()
    {
        var req=new Sys.Net.WebRequest();
        req.set_url("Server.aspx");
        req.set_httpVerb("Post");
        req.add_completed(onCompleteCallback);
        var body="Name="+encodeURIComponent($get("Text1").value);
        req.set_body(body);
        req.get_headers()["Content-Length"]=body.length;
        req.invoke();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    </div>
    <input id="Text1" type="text" />

    <input id="Button1" type="button" value="提交"  onclick="call()"/>
    <div id="div1"></div>
    </form>
</body>
</html>

伺服器相應檔案Server.aspx代碼

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.Write("Hello,"+Request["name"]);
}

執行結果

體會:

回呼函數寫法

function onCompleteCallback(executor,eventArgs)

設定POST方式請求伺服器

req.set_httpVerb("Post");

將資料放到Post內部

var body="Name="+encodeURIComponent($get("Text1").value);
req.set_body(body);

Form中的get和post方法,在資料轉送過程中分別對應了HTTP協議中的GET和POST方法。二者主要區別如下: 
1、Get是用來從伺服器上獲得資料,而Post是用來向伺服器上傳遞資料。 
2、Get將表單中資料的按照variable=value的形式,添加到action所指向的URL後面,並且兩者使用“?”串連,而各個變數之間使用“&”串連;Post是將表單中的資料放在form的資料體中,按照變數和值相對應的方式,傳遞到action所指向URL。 
3、Get是不安全的,因為在傳輸過程,資料被放在請求的URL中,而如今現有的很多伺服器、Proxy 伺服器或者使用者代理程式都會將請求URL記錄到記錄檔中,然後放在某個地方,這樣就可能會有一些隱私的資訊被第三方看到。另外,使用者也可以在瀏覽器上直接看到提交的資料,一些系統內部訊息將會一同顯示在使用者面前。Post的所有操作對使用者來說都是不可見的。 
4、Get傳輸的資料量小,這主要是因為受URL長度限制;而Post可以傳輸大量的資料,所以在上傳檔案只能使用Post(當然還有一個原因,將在後面的提到)。 
5、Get限制Form表單的資料集的值必須為ASCII字元;而Post支援整個ISO10646字元集。 
6、Get是Form的預設方法。 

檢查XmlHttp是否返回成功

if(executor.get_responseAvailable())

以下是Sys.Net.WebRequestExecutor 類

Sys.Net.WebRequestExecutor Class Members

Name

Description

  • Sys.Net.WebRequestExecutor.abort Method

Stops additional processing of the current request.

  • Sys.Net.WebRequestExecutor.executeRequest Method

Executes a Web request.

  • Sys.Net.WebRequestExecutor.getAllResponseHeaders Method

Gets all the response headers for the current request.

  • Sys.Net.WebRequestExecutor.getResponseHeader Method

Gets the value of a specific response header based on the header's name.

  • Sys.Net.WebRequestExecutor aborted Property

Gets a value indicating whether the request associated with the executor was aborted.

  • Sys.Net.WebRequestExecutor responseAvailable Property

Gets a value indicating whether the request completed successfully.

  • Sys.Net.WebRequestExecutor responseData Property

Gets the text representation of the response body.

  • Sys.Net.WebRequestExecutor started Property

Gets a value indicating whether the executor has started processing the request.

  • Sys.Net.WebRequestExecutor statusCode Property

Gets a success status code.

  • Sys.Net.WebRequestExecutor statusText Property

Gets status information about a request that completed successfully.

  • Sys.Net.WebRequestExecutor timedOut Property

Gets a value indicating whether the request timed out.

  • Sys.Net.WebRequestExecutor xml Property

Attempts to get the response to the current request as an XMLDOM object.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.