Asp.net combined with Ajax to verify whether the user name exists

Source: Internet
Author: User
Tags html header

1. Use the JavaScript js file to verify whether the user name exists.
Copy codeThe Code is as follows:
Var ajax = function (option)
{
Var request;
Var createRequest = function ()
{
// Var request;
If (window. XMLHttpRequest)
{
Request = new XMLHttpRequest ();
}
Else
{
Try
{
Request = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e)
{
Request = new ActiveXObject ("Msxml2.XMLHTTP ");
}
}
Return request;
}
Var sendRequest = function ()
{
Request = createRequest ();
/* --------- Difference between GET and POST ------------
1. get adds the parameter data queue to the URL referred to by the ACTION attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL.
Post uses the HTTP post mechanism to place fields in the form and their content in the html header and send them to the URL address referred to by the ACTION attribute. You cannot see this process.
2. For the get method, the server uses Request. QueryString to obtain the variable value,
For the post method, the server uses Request. Form to obtain the submitted data.
You can use Request to obtain parameters in either of the two methods.
3. The data volume transmitted by get is small and cannot exceed 2 kb.
The amount of data transmitted by post is large, which is generally not restricted by default.
4. Low get security and high post security.
5. When submitting a form, we usually use the post method. When we want to transmit a large data file, we need to use post.
When the passed value only needs to be in the parameter mode (this value is not greater than 2 kb), use the get method.
*/
Request. open ("GET", option. url, true );
// Request. open ("POST", option. url, true );
// Set this attribute before sending a request to obtain the preparation status on the server
Request. onreadystatechange = ResponseRequest;
Request. send (null );
}
/*
Request. readyState = 4 indicates that the server has received a response.
Request. status = 200, HTTP server response status value, indicating that everything goes smoothly
HTTP readiness
0: the request is not sent (before open () is called ).
1: The request has been created but has not been issued (before sending () is called ).
2: The request is being processed (the content header can be obtained from the response ).
3: The request has been processed, and some data is usually available in the response, but the server has not yet completed the response.
4: The response has been completed. You can access the server response and use it.
*/
Var ResponseRequest = function ()
{
Alert ("HTTP Readiness:" + request. readyState );
If (request. readyState = 4)
{
If (request. status = 200)
{
Alert ("everything goes well! ");
Option. Success (request );
}
Else
{
Alert ("error, error message:" + request. status );
Option. Failure (request );
}
}
}
SendRequest ();
}
// Determine whether the input value exists
Function getIS ()
{
/*
1. the url is the page to be linked and the pass-through value. The pass-through value is used for dynamic page execution.
2, Default. aspx is the page for execution
3. name is the name of the parameter passed in.
4, document. getElementById ('text1'). The parameter value passed by value
5. The message returned from the server after message. responseText is successful
*/
Var option =
{
Url: "Default. aspx? Name = "+ document. getElementById ('text1'). value,
Success: function (message)
{
Alert (message. responseText );
}
};
New ajax (option );
}

Aspx File
Copy codeThe Code is as follows:
If (Request ["name"]! = Null)
{
This. Response. Clear ();
String name = Request ["name"]. ToString ();
If (name = "1 ")
{
Response. Write ("the user name already exists. Please enter another user name! ");
}
Else
{
Response. Write ("this user name is not registered. You can use it! ");
}

This. Response. End ();
}

2. Implemented through Jquery:
Copy codeThe Code is as follows:
$ (Document). ready (function (){
$ ("# Button1"). click (function (){
$. Ajax ({
Type: "GET ",
Url: "ResponsePage. aspx? Name = "+ document. getElementById ('text1'). value,
Success: function (message ){
Alert (message );
}

});

});
});

Related Article

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.