Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of JQuery+AJAX call background in ASP.NET

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "sample analysis of JQuery+AJAX call background in ASP.NET", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "example analysis of JQuery+AJAX call background in ASP.NET".

Implementation process:

WebForm Code:

Function login () {var name = document.getElementById ("username"). Value; / / get the username var password = document.getElementById ("userpassword"). Value; / / get the password var params ='{name: "'+ name +'", password: "'+ password +'"}' / / pass the username and password as parameters to $.ajax ({url: "LoginMobile.aspx/test", / / call the background method data: params, type: "post", dataType: 'text', contentType: "application/json Charset=utf-8 ", / / set the type, be sure not to lose success: function (data) {if (data = ='{" d ": true}') {/ / pay attention to the judgment condition _ window.location =".. / Order/OrderMobile.aspx ";} else {mui.toast (" wrong username or password! ") ;})

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 () = "ordinary user") {Flag = true;} return Flag;}

Pay special attention to:

1, in the webForm page to try Ajax technology to call the background method, be sure to 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 a feature to the method declaration [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 mui, which is not much different from the usual ajax, except that the writing form is a little different. The interface implemented with 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 for the interaction between foreground and background, and flexible use will bring us great help. Of course, different settings and uses should be made according to different environments.

The above is all the contents of the article "sample Analysis of JQuery+AJAX call background in ASP.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report