The $.post of Asp.net+jquery.ajax

Source: Internet
Author: User

Some time ago a bit of deviation, after rice teacher and master's patient guidance, finally realize their own problems, now return to the regular route, continue B/S study.

After nearly half a month of edification, the jquery slowly has a sense of intimacy. At that time I interviewed a clear, asked him to read the jquery video what feeling, a clear said: "Can understand, can understand, do not know where to start." This may be the reason for most beginners, now I use a simple login page to help you understand how Jquery.ajax works.

First we create an HTML page for interacting with the user, with the following code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
then we use jquery as the HTML login button to bind the point-click event, the code is as follows:
When the form loads, the login button is tied to the fixed-point event $ (document). Ready (function () {    $ ("#login"). Click (function () {            $.post (' Login.ashx ', {            //Parameter one: User name            userName: $ ("#userName"). Val (),            //Parameter two: password            passWord: $ ("#passWord"). Val ()        },        // Callback functions function            (data) {                //Output                alert (data);            },        //return type            "text"            )    ;});
jquery.post (URL,[data],[callback],[Type]) is used to interact with the server, it has 4 parameters, URL: Tells Ajax to submit data to which page to process, data: parameters to pass during the commit process, callback: callback function, to receive the server's return value, type: Return the value of the types, typically text or JSON.

   

using System;using system.collections.generic;using system.linq;using system.web;namespace Demo{//<summary>//L  Ogin's summary description///</summary> public class Login:ihttphandler {public void ProcessRequest (HttpContext Context) {context.            Response.ContentType = "Text/plain"; Receives an AJAX-passed parameter string userName = Context. request["UserName"].            ToString (); String PassWord = context. request["PassWord"].            ToString (); The AJAX passed parameters are passed back to Ajax if (UserName = = "Admin" && PassWord = = "Admin") {Co ntext.            Response.Write ("Login Successful"); } else {context.            Response.Write ("Login failed!");            }} public bool IsReusable {get {return false; }        }    }}

Finally, we look at the demo running effect:

Through the two login process we can find that when the user name, password is "1" when the box is "Login Failed", when the user name, password is "admin", the box is "login success."

Looking back on the whole realization process, it's not hard to find Jquery.ajax receive the value of the page user input, and then through the Post method to submit to the background server, the background server receives the post submission request, processing, processing, and then back to the Ajax,jquery.ajax through the Post method callback function, processing the data returned by the server, complete a user request 。

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.