ASP.NET Web 前台Ajax傳遞JSON

來源:互聯網
上載者:User

標籤:receive   load   ejs   發送請求   流程   webform   jquer   text   交換   

     Json 作為一種輕量級的資料交換格式,主要用於和伺服器之間進行交換資料,其資料格式類似於 索引值組合形式的數組。

     在Web 中使用Ajax傳遞Json資料時候,就不得不先提一提Form表單,web前台介面中使用的控制項預設是嵌在 form中的,而在form下使用asp:button控制項,被HTML編譯後預設類型為Submit,如此就先於Ajax一步提交了表單,導致Json資料無法正常傳遞到後台,合理的解決辦法是 給Button 設定 OnClientClick="return false;" 讓其不提交。

前台Post代碼:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title><script src="JS/jquery-3.1.1.min.js" type="text/javascript"></script><script type="text/javascript">        $(function () {            $("#btnSub").click(function () {                $.ajax({                    type: "post",                    contentType: "application/json; charset=utf-8",                    dataType: "json",                    url: "WebForm1.aspx/ReceiveJson",                    data: "{‘Name‘:‘郎中令‘,‘Age‘:‘200‘}",                    success: function (data) {                                             var result = data.d;                        alert(result);                                           },                    error: function () {                        alert("發送失敗");                    }                });            });        });                  </script></head><body>    <form id="form1" runat="server">    <div>                   <asp:Button ID="btnSub" runat="server" Text="Ajax" ClientIDMode="Static" OnClientClick="return false;"></asp:Button>            </div>          </form></body></html>

後台代碼:前台返回的資料是幕後處理的資料,如,後台只返回Hello 前台返回的資料也將是Hello

        //參數名必須與前台JSon鍵名一致
//方法必須是是靜態 static
[WebMethod] public static string ReceiveJson(string Name, string Age) { return "Name:" + Name + " " + "Age:" + Age; }

大致流程  前台發送請求---後台接受資料--分析處理+返回資料--前台接收

 

 

另外說說關於Form表單,提交方式為Post、Get ,兩者區別

Post:預設情況下,如果設定了Action ,Google會在Network下找到傳遞的參數

Get:提交的參數會在導覽列中顯示,故大資料/檔案/隱私 不可傳遞

建立一HtmlPage1.html,如下post發送()

<body>  <form enctype="multipart/form-data" action="WebForm1.aspx" method="post">         <label for="txtname">帳號:</label>         <input type="text" id="txtname" name="username" /><br />         <input type="submit"  value="提交"/></form></body>

然後在 WebForm1.aspx 的Page_Load 中接受Form表單提交的資料

        protected void Page_Load(object sender, EventArgs e)        {            //取得表單中所有的鍵名            string[] name = Request.Form.AllKeys;            for (int i = 0; i < name.Length; i++)            {                //取得表單中所有的值                string[] value = Request.Form.GetValues(i);                //遍曆輸出 ‘鍵‘ 和 ‘值‘                 for (int j = 0; j < value.Length; j++)                {                    Response.Write(name[i] + "=" + value[j] + "<br/>");                }            }        }

 

ASP.NET Web 前台Ajax傳遞JSON

聯繫我們

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