Home  >  Article  >  Backend Development  >  JQuery+AJAX calling background in ASP.NET

JQuery+AJAX calling background in ASP.NET

高洛峰
高洛峰Original
2016-12-16 16:19:411930browse

When I was working on the mobile version of the ordering system, I encountered a problem. When implementing the login function, I had to call the background method for verification and judgment. We use webForm for development. Under normal circumstances, as long as the button binding method is used, the front-end and back-end correspondence can be achieved. However, after applying the MUI style on the mobile phone, it is no longer applicable to 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; //获取用户名
      var password = document.getElementById("userpassword").value; //获取密码
      var params = '{name:"' + name + '",password:"' + password + '"}'; //将用户名和密码作为参数传过去
      $.ajax({
        url: "LoginMobile.aspx/test", //调用后台方法
        data: params,
        type: "post",
        dataType: 'text',
        contentType: "application/json; charset=utf-8", //设置类型,注意一定不能丢
        success: function (data) {          
          if (data == '{"d":true}') { //注意判断条件
            window.location = "../Order/OrderMobile.aspx";
          } else {            
            mui.toast("用户名或密码错误!");            
          }
        }
      });
  
    }

Backend code:

[WebMethod]
    public static bool test(string name,string password) {
      //实例化登录业务逻辑类
      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() == "普通用户")
          {
           Flag = true;
          }
        }       
         
      }
      return Flag;
    }

Special attention:

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

2. In the background method

                                                                                                                                                                                                          ‐                                     to             The number of parameters should also be the same as the parameters of the method.

Of course, you can also use the free ajax technology in mui. Its usage is not much different from ordinary ajax, except that the writing form is a little different. The interface form implemented using 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("用户名或密码错误!");            
          }
        }
      })

ajax Technology is also a good way to interact between the front and backend, and its flexible use will bring us huge help. Of course, different settings and uses must be made according to different environments.

For more articles related to JQuery+AJAX calling background in ASP.NET, please pay attention to PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn