. NET Ajax request data submission instance,. netajax request instance
This document describes how to submit. NET Ajax request data. Share it with you for your reference. The details are as follows:
Copy codeThe Code is as follows: <% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage <dynamic>" %>
<Head runat = "server">
<Title> ajax request </title>
<Link type = "text/css" rel = "stylesheet" href = "/Content/style.css"/>
<Script type = "text/javascript" src = "/Scripts/jquery-1.8.3.min.js"> </script>
<Script type = "text/javascript" src = "/Scripts/js. js"> </script>
</Head>
<Body>
<! -- Top + logo + navigation -->
<Div class = "logo_box">
<Div id = "logo">
<A title = "ajax request"> ajax request </a> </div>
</Div>
<! ---->
<Div class = "loginCon">
<Div class = "loginBanner">
</div>
<Div class = "loginBox">
<H2>
<Span class = "fl"> member logon </span> <span class = "newUser"> no account? <A href = '<% = Url. Action ("Register", "Account") %>'> Register now </a> </span>
<Form id = "formData">
<Div class = "loginForm">
<Div class = "inputBox">
<Input type = "text" name = "user" value = "user name/mobile phone number" class = "userId"/>
</Div>
<Div class = "inputBox">
<Input type = "text" value = "password" class = "textStyle"/>
<Input type = "password" name = "pwd" class = "passwordStyle none"/>
</Div>
<Div class = "warn"> the user name or password is incorrect! </Div>
<Div class = "remember">
<Label>
<Input type = "checkbox" name = "remembered" checked/>
Automatic Logon </label>
<A class = "forget" href = '<% = Url. Action ("ResetPwd", "Login") %>'> forgot password? </A>
</Div>
<Input class = "loginBtn" type = "button" value = "Logon"/>
</Div>
</Form>
</Div>
</Div>
</Body>
<Script type = "text/javascript">
$ (Function (){
$ ('. UserId,. passwordStyle'). on ('keyup', function (e ){
If (e. keyCode = 13 ){
$ ('. LoginBtn'). trigger ('click ');
}
});
$ ('. LoginBtn'). on ('click', function (){
$ (". Warn"). hide ();
Var pwd = $ ('. passwordstyle'). val ();
If (pwd = ''){
$ (". Warn" mongo.show(mongo.html ('enter your password ');
Return false;
}
Var data = $ ("# formData"). serialize ();
$. Post ("/login/checkLoginInfo", data, function (ajaxObj ){
// Return content {status: 1 (success)/0 (fail ),}
If (ajaxObj. status = 0 | status = null ){
$ (". Warn" mongo.show(mongo.html ('user name or password is incorrect! ');
} Else {
// The logon is successful and the page is displayed
Window. location = '/memberCenter/Index ';
}
}, "Json ");
});
});
</Script>
</Html>
Controller
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using System. Text;
Namespace bigtree. Controllers
{
Using bigtree. Models;
Using bigtree. Model;
Using bigtree. lib;
Using System. Net. Mail;
Using System. Text. RegularExpressions;
Public class LoginController: Controller
{
Public ActionResult Index ()
{
Return View ();
}
/// <Summary>
/// Check Logon
/// </Summary>
/// <Param name = "f"> </param>
/// <Returns> </returns>
[HttpPost]
Public ActionResult CheckLoginInfo (FormCollection f)
{
Try
{
// Post: user, pwd, remembered
String user = f ["user"]. Trim ();
String pwd = f ["pwd"]. Trim ();
String remembered = f ["remembered"]. Trim ();
JsonResult res = new JsonResult ();
If (string. IsNullOrEmpty (user) | string. IsNullOrEmpty (pwd ))
{
Res. Data = new {status = 0 };
}
// MD5 encrypted password
Pwd = System. Web. Security. FormsAuthentication. HashPasswordForStoringInConfigFile (pwd, "md5"). ToLower ();
// Read from the database
Common. WebUser account = MemberInfoService. GetMemberIdForCheck (user, pwd );
If (account = null)
{
Res. Data = new {status = 0 };
}
Else
{
// {Status: 1 (success)/0 (fail ),}
Res. Data = new {status = 1 };
// Todo
FunSession. SetSession (account );
// Remember to log on
If (remembered = "on ")
{
HttpCookie cookie = new HttpCookie ("LoginInfo", account. Id. ToString ());
// Valid for 3 days
Cookie. Expires. AddDays (3 );
Response. Cookies. Add (cookie );
}
Else
{
HttpCookie cookie = new HttpCookie (account. Id. ToString (), account. Id. ToString ());
// Invalidate
Cookie. Expires. AddYears (-1 );
Response. Cookies. Add (cookie );
}
}
Return res;
}
Catch (Exception ex)
{
Throw ex. InnerException;
}
}
}
}
I hope this article will help you with the. NET program design.