An example of jQuery ajax calling C # in the background

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.