Asp. NET in Jquery+ajax call back

Source: Internet
Author: User
Tags call back
When making the ordering system mobile phone side, encountered a problem, to implement the login function, I want to invoke the background method to verify and judge. We use WebForm to develop, under normal circumstances, as long as the method of binding buttons, the front and back of the corresponding can be achieved. However, after applying the MUI style to the phone, this is not the case. 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;//Get user name      var password = document.getElementById ("UserPassword"). Value; Get 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 type, note must not be lost        success:function (data) {                    if (data = = ' {" D ": true} ') {//Note to determine the condition            Window.location = ". /order/ordermobile.aspx ";          } else {                        Mui.toast ("username or password is wrong! ");                      }        }      });      }

Background code:

[webmethod] public static bool Test (string name,string password) {//Instantiate login business logic      Class CARDBLL CARDBLL = new CARDBLL ();      USERBLL user = new Userbll ();      Page page = (page) System.Web.HttpContext.Current.Handler;        BOOL Flag = false; General user 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; }

Special attention:

1, in WebForm page trial Ajax Technology Call backstage method, must add ContentType: "Application/json; Charset=utf-8 ". Otherwise, you cannot invoke the background method. Type is "Post".

2, in the background method

First, the backstage method must be static;

Second, the method declaration is to be coupled with an attribute [System.Web.Services.WebMethod ()];

Thirdly, the number of arguments passed should be the same as the parameters of the method.

Of course, you can also use the free AJAX Technology in MUI, its use is not much different from the usual Ajax, but the form of writing is a little different, the use of MUI implementation of the interface as follows:

Mui.ajax (' Loginmobile.aspx/test ', {        data:params,        dataType: ' Text ',        type: ' Post ',        contentType: " Application/json; Charset=utf-8 ",        success:function (data) {                     if (data = = ' {" D ": true} ') {            window.location ="). /order/ordermobile.aspx ";          } else {                        Mui.toast ("username or password is wrong! ");                      }        }      })

Ajax technology for the interaction between the front and back is also a good way, flexible use will bring us great help. Of course, depending on the environment for different settings and use.


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.