Asp.net common submission methods

There are no more than two types of submission: Express submission and ajax submission


1: Form submission

     html

  1. <form action="TestWebspx.aspx"  method="post">  
  2.         <input  type="text" name="username"/>  
  3.         <input  type="submit" value="submit"/>  
  4.     </form>  
Get the submitted data in the background and return it to the foreground

  1. public partial class TestWebspx : System.Web.UI.Page  
  2.     {  
  3.         protected void Page_Load(object sender, EventArgs e)  
  4.         {  
  5.             string username = Request["username"];  
  6.             Response.Write("your input:" + username);  
  7.         }  
  8.     }  

2: Ajax processing


Method 1:

Front end

  1. var kmtcb = {}  
  2.   
  3.        kmtcb.search = function ()  
  4.        {  
  5.            var qyport = $("#qyport").val();  
  6.            var carrier = $("#carrier").val();  
  7.            var number = $("#number").val();  
  8.   
  9.   
  10.            $.ajax({  
  11.                url: "get_b.aspx?act=Search",  
  12.                type: "post",  
  13.                data: { qyport: qyport, carrier: carrier,number:number},  
  14.                success: function (result) {  
  15.                    var dataObj = eval("(" + result + ")");//Convert to json object  
  16.                    alert(dataObj.Carrier);  
  17.                    alert(dataObj.Qyport);  
  18.                    alert(dataObj.Number);  
  19.                },  
  20.                error: function (result)  
  21.                {  
  22.                    console.log(result);  
  23.                    alert(result);  
  24.                }  
  25.            })  
  26.        }  
Back end

   

  1. public void Search()   
  2.     {  
  3.           
  4.   
  5.         string qyport = Request["qyport"];  
  6.         string carrier = Request["carrier"];  
  7.         string number = Request["number"];  
  8.   
  9.         T t = new T();  
  10.         t.Carrier = "1234";  
  11.         t.Qyport = "aaaa";  
  12.         t.Number = "yunjia001";  
  13.         string jsonstr = JsonConvert.SerializeObject(t);  
  14.   
  15.         Response.Clear();  
  16.         Response.Write(jsonstr);  
  17.         Response.End();  
  18.     }  



Method 2:

  1. $(function () {  
  2.   
  3.            $.ajax({  
  4.                type: "post",  
  5.                url: "TestWebspx.aspx/TestJson",  
  6.                contentType: "application/json;charset=utf-8",  
  7.                data: "{'msg':'hello'}",// data: { key: '123', type: 'S' }  
  8.                dataType: "json",  
  9.                success: function (result)  
  10.                {  
  11.                    var dataObj = eval("(" + result.d + ")");  
  12.                    alert(dataObj.msg);  
  13.                }  
  14.            });  
  15.        })  
This is because Microsoft framework returns a {"d": "data returned in the background"} data by default

So we need to use result.d and eval to transfer the returned json object, or use jquery's $. parseJSON("str");

Backstage

  1. [WebMethod]  
  2.         public static string TestJson(string msg)   
  3.         {  
  4.             return "{msg:123}";  
  5.         }  
If the background method must be static and the [WebMethod] attribute must be added, an exception will be thrown

  

       

There is no problem with the post request at this time, but it will appear if you need to modify it to get mode

  

Just add the background method and the [ScriptMethod(UseHttpGet = true)] attribute

  1. [WebMethod]  
  2.        [ScriptMethod(UseHttpGet = true)]  
  3.        public static string TestJson(string msg)   
  4.        {  
  5.            return "{msg:123}";  
  6.        }  

http://www.cnblogs.com/acles/articles/2385648.html

Original blog address: https://blog.csdn.net/aojiancc2/article/details/19075337

Keywords: JSON Attribute JQuery

Added by mjdamato on Tue, 31 Mar 2020 13:02:17 +0300