Asp. NET in Jquery+ajax call background _ practical skills

Source: Internet
Author: User

When making a reservation system mobile phone, encountered a problem, the implementation of login function, I want to call the background of the method to verify and judge. We are using WebForm to develop, under normal circumstances as long as the method of binding the button, front and rear table 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, the MUI also bring Ajax technology.

Implementation process:

WebForm Code:

function login () {       
      var name = document.getElementById ("username"). value;//get username 
      var password = document.getElementById ("UserPassword"). Value; Get password 
      var params = ' {name: ' + name + ' ', Password: ' + password + ' '} '; Pass the username and password as arguments past 
      $.ajax ({ 
        URL: "Loginmobile.aspx/test",//Call back-end method 
        Data:params, 
        type: "Post" 
        , DataType: ' text ', 
        contentType: "Application/json;" Charset=utf-8 ",//setting type, note must not throw 
        success:function (data) {           
          if (data = = ' {" D ": true} ') {//Pay attention to the condition 
            of judgment Window.location = ".. /order/ordermobile.aspx "; 
          } else {             
            mui.toast ("User name or password is wrong!") ");             
          } 
        } 
      }); 
 
    } 

Background code:

[WebMethod] public static bool Test (string name,string password) {//Instantiated login business logic class CARDBLL CARDBLL = new 
      CARDBLL (); 
      USERBLL user = new Userbll (); 
      Page page = (page) System.Web.HttpContext.Current.Handler; 
 
      BOOL Flag = false; The 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 to try Ajax technology call back-end method, must add ContentType: "Application/json; Charset=utf-8 ". Otherwise, the background method cannot be invoked. Type is "Post".

2, the Background method

First, the backstage method must be static;

Second, the method declaration should be added with characteristics [System.Web.Services.WebMethod ()];

Third, the number of parameters passed should also be the same as the parameters of the method.

Of course, you can also use the free AJAX technology in the MUI, its usage is not much different from the usual Ajax, but the form of writing is a little different, using the MUI implementation of the interface form is 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 ("User name or password is wrong!") ");             
          } 
        } 
      

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

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.