In ASP. NET, The JQuery + AJAX call backend, jqueryajax

Source: Internet
Author: User

In ASP. NET, The JQuery + AJAX call backend, jqueryajax

A problem occurs on the mobile phone end of the ordering system. When implementing the login function, I need to call the background Method for verification and judgment. We use webForm for development. Under normal circumstances, we only need to bind the button method, and the frontend and backend can be implemented accordingly. However, after the MUI style is applied on the mobile phone end, it is not suitable for this situation. Based on this problem, we use JQuery + Ajax technology. In fact, MUI also comes with ajax technology.

Implementation process:

WebForm code:

Function login () {var name = document. getElementById ("username "). value; // obtain the user name var password = document. getElementById ("userpassword "). value; // obtain the password var params = '{name: "' + name + '", password: "' + password + '"}'; // pass the user name and password as parameters $. ajax ({url: "LoginMobile. aspx/test ", // call the background method data: params, type:" post ", dataType: 'text', contentType:" application/json; charset = UTF-8 ", // set the type. Note that success: fu cannot be lost. Nction (data) {if (data = '{"d": true}') {// identify the condition window. location = ".. /Order/OrderMobile. aspx ";} else {mui. toast ("incorrect user name or password! ");}}});}

Background code:

[WebMethod] public static bool test (string name, string password) {// instantiate the login business logic class CardBll cardBll = new CardBll (); userBll user = new userBll (); page page = (Page) System. web. httpContext. current. handler; bool Flag = false; // if (name. length> 5) {Flag = cardBll. isExist (name, password); if (Flag = true) {System. web. httpContext. current. session ["Admin"] = name; // Session ["Admin"] = name; // Session ["Username"] = cardBll. username (TxtName. text. trim (), TxtPassword. text. trim (); System. web. httpContext. current. session ["Username"] = cardBll. username (name); System. web. httpContext. current. session ["cardLevel"] = cardBll. cardLevel (name); if (System. web. httpContext. current. session ["cardLevel"]. toString () = "normal user") {Flag = true ;}} return Flag ;}

Note:

1. When using Ajax technology to call the background method on the webForm page, you must add contentType: "application/json; charset = UTF-8 ". Otherwise, the backend method cannot be called. The type is "Post ".

2. Background Method

First, the background method must be static;

Second, add the feature [System. Web. Services. WebMethod ()] to the method declaration;

Third, the number of parameters passed must be the same as that of the method.

Of course, you can also use the free ajax technology in mui. Its usage is not much different from the normal ajax, but the writing format is a little different. The interface form implemented by using MUI is as follows:

Mui. ajax ('loginmobile. aspx/test', {data: params, ype: 'text', type: 'post', contentType: "application/json; charset = UTF-8", success: function (data) {if (data = '{"d": true}') {window. location = ".. /Order/OrderMobile. aspx ";} else {mui. toast ("incorrect user name or password! ");}}})

Ajax technology is also a good way to interact between the front and back, and flexible use will bring us great help. Of course, you must also set and use different environments.

Articles you may be interested in:
  • JQuery uses ajax to register user instances (backend asp.net)
  • Example of three frameworks (aspnet, Jquery, and ExtJs) of DropDownList + Ajax in asp.net
  • Jquery + Ajax call webService instance code (asp.net)
  • Jquery + ajax request data is displayed on the GridView (asp.net)
  • Use Jquery Ajax in Asp.net to detect User Registration (verify whether the user name is saved)
  • Code for transmitting and receiving DataTable using Jquery Ajax in Asp.net
  • In asp.net, jquery ajax + WebService + json is used to implement the Code of refreshing the background value.
  • Ajax verification implementation code for jQuery Validation-Engine in ASP. NET
  • Example of asp.net that uses Ajax and Jquery to transmit parameters to the background and return values in the foreground
  • Jquery. Ajax () method to call Asp. Net background method Parsing

Related Article

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.