Asp.net XMLHttpRequest implements pre-registration verification

Source: Internet
Author: User

Add two new projects in the test environment VS2008
1: DEMO3.ASPX Copy codeThe Code is as follows: // query string and SEND server Parameters
Function createQueryString (){
Var userName = document. getElementById ("userName"). value;
Var querystring = "userName =" + userName;
Return querystring;
}
Function RunAjax (){
// Refer to 'How to create XMLHttpRequest object Code' in DEMO1.HTML from meaningful examples in the book'
CreateXMLHttpRequest ();
Var queryString = "IsHaveUser. ashx? ";
QueryString = queryString + createQueryString ()
+ "& TimeStamp =" + new Date (). getTime (); // append a timeStamp to the URL to prevent browser caching
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("GET", queryString, true );
XmlHttp. send (null );
Document. getElementById ('validatemessage'). innerText = 'checking. Please wait ...';
}
Function handleStateChange (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var result = xmlHttp. responseText;
Document. getElementById ("validateMessage"). innerText = result;
If (result = "Congratulations! You can register ..."){
SetTimeout ("document. getElementById ('validatemessage'). innerText =''; ", 2000 );
}
}
}
}
<Input id = "userName"/> <input id = "IsHaveUser" type = "button" value = "check whether a user is registered" onclick = "RunAjax (); "/> <span id =" validateMessage "> </span>

2: IsHaveUser HandlerCopy codeThe Code is as follows: public class IsHaveUser: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
System. Threading. Thread. Sleep (2000 );
String userName = context. Request. QueryString ["userName"];
Bool result = reads the database and performs an operation to determine whether the user name exists. A boolean value is returned, omitting the code.
If (result)
{
Context. Response. Write ("Congratulations! You can register ...");
}
Else {context. Response. Write ("this user exists. Replace ...");}
}
Public bool IsReusable {get {return false ;}}
}

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.