Asp.Net+Jquery.Ajax詳解3-$.get和$.post

來源:互聯網
上載者:User

目錄(已經更新的文章會有串連,從7月25日開始,每2到3天更新一篇):

Asp.Net+Jquery.Ajax詳解1-開篇(2012.07.25發)

Asp.Net+Jquery.Ajax詳解2-$.Load(2012.07.26發)

Asp.Net+Jquery.Ajax詳解3-$.get和$.post(2012.07.30發)

Asp.Net+Jquery.Ajax詳解4-$.getJSON(2012.07.31發)

Asp.Net+Jquery.Ajax詳解5-$.getScript(2012.08.04發)

Asp.Net+Jquery.Ajax詳解6-$.ajaxSetup(2012.08.06發)

Asp.Net+Jquery.Ajax詳解7-全域Ajax事件(2012.08.09發)

Asp.Net+Jquery.Ajax詳解8-核心$.ajax(2012.08.12發)

Asp.Net+Jquery.Ajax詳解9-serialize和serializeArray(2012.08.15發)

Asp.Net+Jquery.Ajax詳解10-JSON和XML+寫在最後(2012.08.20發,結束啦!)

 

 

jQuery.get(url, [data], [callback], [type])

通過遠程 HTTP GET 請求載入資訊

 

參數——

url:為請求的url地址 

data:待發送 Key/value 參數。

callback:載入成功時回呼函數。

type:返回內容格式,xml, html, script, json, text, _default。

 

get()方法提供了回呼函數(callback),該函數有三個參數:responseText,textStatus,XMLHttpRequest,分別代表請求返回的內容、請求狀態和XMLHttpRequest對象。

 

 $.get("Data/GetServiceInfo.aspx",function(responseText,textStatus,XMLHttpRequest){//responseText:請求返回的內容 //textStatus:請求狀態:success、error、notmodified、timeout//XMLHttpRequest:XMLHttpRequest對象 });

 

dataType 規定預計的伺服器響應的資料類型。預設地,jQuery 將智能判斷。可能的類型:"xml" "html" "text""script" "json" "jsonp" 

jQuery.post與Jquery.get最大的區別在於,前者通過遠程 HTTP POST 請求載入資訊。後者通過遠程HTTP GET請求載入資訊。其他部分基本相同。

 

執行個體:

用戶端——

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryAjaxGetPost.aspx.cs" Inherits="JqueryAjaxTest.JqueryAjaxGet" %><!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>Jquery Ajax Test</title>       <%--引入Jquery庫--%>    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>    <script type="text/javascript">        $(function () {            //為各個按鈕綁定事件            $("#TestGet").bind("click", GetWithCallback);            $("#TestPost").bind("click", PostWithCallback);        });        //測試get,使用回呼函數        //注意:get()方法提供了回呼函數(callback),該函數有三個參數,分別代表請求返回的內容、請求狀態和XMLHttpRequest對象        function GetWithCallback(event) {            $.get("Data/GetServiceInfo.aspx", { "param": "TestGet-Callback" }, function (responseText, textStatus, XMLHttpRequest) {                $("#result").html("回呼函數在起作用,結果:" + responseText);            });        }        //測試post,使用回呼函數        function PostWithCallback(event) {            $.post("Data/GetServiceInfo.aspx", { "param": "TestPost-Callback" }, function (responseText, textStatus, XMLHttpRequest) {                $("#result").html("回呼函數在起作用,結果:" + responseText);            });        }          </script></head><body>    <form id="form1" runat="server">    <div>              <input id="TestGet" type="button" value="測試jquery.get" />            <input id="TestPost" type="button" value="測試jquery.post" />        <div id="result">        </div>    </div>    </form></body></html>

服務端——

 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace JqueryAjaxTest.Data{    public partial class GetMethodInfo : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string param = "";            //擷取參數            if (!String.IsNullOrEmpty(HttpContext.Current.Request["param"]))            {                param = HttpContext.Current.Request["param"];            }                        //清空緩衝區            Response.Clear();            //將字串寫入響應輸出資料流            Response.Write("Http請求的方式為:"+Request.HttpMethod.ToUpper()+"; 傳遞過來的參數為:"+param);            //將當前所有緩衝的輸出發送的用戶端,並停止該頁執行            Response.End();        }    }}

 

補充一點:

 

對比load和get:

 

$.load()是最簡單的從伺服器擷取資料的方法。它幾乎與 $.get() 等價,不同的是它不是全域函數,並且它擁有隱式的回呼函數。當偵測到成功的響應時(比如,當 textStatus 為 "success" 或 "notmodified" 時),$.load() 將匹配元素的 HTML 內容設定為返回的資料。

 

這也是為什麼我們再上一篇的執行個體中這麼用 $("#result").load()。而在這一篇的執行個體中這麼用$.get()

 

第3篇結束,一會發第4篇

 

 

 

相關文章

聯繫我們

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