Ajax principles:
The ajax principle is to implement a new local environment and then submit the data to the background program. After receiving the data, the background program enters the processing logic, and then returns the returned value to the user after receiving it by ajax, this complete ajax interaction with the background is complete. Let's take a look at the example below.
JQuery code at the front end
| The code is as follows: |
Copy code |
$ (Function (){
$ ("# WFddlType"). change ( Function (){ $. Ajax ({ Type: "Post ", Url: "feeForm. aspx/FindLeftBudget", // page name/name of the background method to be called Data: "{'feetypeid': '000000', 'costcenterid': '000000'}", // a string in json format transmits the parameter to the backend. The parameter names must be consistent. ContentType: "application/json; charset = utf-8 ", DataType: "json ", Success: function (result ){ $ ("# LeftBudget "). text (result. d); // assign the obtained value to the front-end control. Here, d. If the returned value is not a json string, but only a value, the returned value is included in the property named d. (Strange, I think so too ...) }, Error: function (err ){ Alert (err ); } }); }); }); |
Background C # code
Note: 1. The method must be static. 2. The WebMethod feature must be added at the top of the method.
| The code is as follows: |
Copy code |
[WebMethod] Public static string Find (string feeTypeID, string costCenterID) { If (BudgetControlFacade. Instance. Check (feeTypeID )) { Return BudgetControlFacade. Instance. FindBalance (feeTypeID, costCenterID). ToString (); } Else { Return "+ & infin ;"; } } |
Analysis: in ajax, the core sentence is $. ajax {}, followed by parameters.
Type: "Post". The method submitted to the program is post or get.
Url: "feeForm. aspx/FindLeftBudget", // asp.net file for receiving data
Data: "{'feetypeid': '000000', 'costcenterid': '000000'}", data is transmitted to the background
ContentType: "application/json; charset = utf-8", encoding of sent data
DataType: "json", transfer data type, which corresponds to data.