ASP. JQuery+AJAX call background in NET

  • 2021-07-13 05:01:19
  • OfStack

When I was doing the mobile phone terminal of the ordering system, I encountered a problem. When I realized the login function, I had to call the background method for verification and judgment. We are using webForm development, as long as the normal binding button method, the foreground and backstage correspondence can be achieved. However, after the MUI style is applied to the mobile phone, 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; // 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", // Invoke the background method  
        data: params, 
        type: "post", 
        dataType: 'text', 
        contentType: "application/json; charset=utf-8", // Set the type, note 1 You can't lose it  
        success: function (data) {           
          if (data == '{"d":true}') { // Pay attention to judgment conditions  
            window.location = "../Order/OrderMobile.aspx"; 
          } else {             
            mui.toast(" Wrong username 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; 
 
      //1 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() == " Ordinary users ") 
          { 
           Flag = true; 
          } 
        }        
        
      } 
      return Flag; 
    } 

Pay special attention to:

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

2. In the 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 should also be the same as the parameters of the method.

Of course, you can also use the free ajax technology in mui, which is not much different from the ordinary ajax, but the writing form is a little different. The interface form realized by MUI 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(" Wrong username or password! ");             
          } 
        } 
      }) 

ajax technology is also a good way to interact between front and back, and flexible application will bring us great help. Of course, it should be set up and used differently according to different environments.


Related articles: