asp.net combines Ajax to verify the existence of a user name

  • 2020-05-10 17:57:28
  • OfStack

1. Use JavaScript js file to verify the existence of the user name
 
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(); 
/*---------GET  and  POST The difference between ------------ 
1 ,  get The parameter data queue is added to the submission form ACTION attribute-referent URL , values and the various fields in the form 11 Corresponding to the URL You can see that.  
post Is through the HTTP post Mechanism to place each field in the form with its content HTML HEADER Within the 1 Up to ACTION attribute-referent URL Address. Users can't see the process.  
2 ,   for get The way is used on the server side Request.QueryString Get the value of the variable,  
 for post The way is used on the server side Request.Form Get the submitted data.  
 Arguments in both ways can be used Request To obtain.  
3 ,  get The amount of data transmitted is small and cannot be greater than 2KB .  
post The amount of data transmitted was large, 1 It is generally default to be unrestricted.  
4 ,  get Security is very low, post High security.  
5 ,   We usually use it when we submit a form post way , When we want to transmit 1 Two large data files , Need to use post .  
 When the value is passed only in the form of parameters ( This value is not greater than 2KB) when , with get The way can.  
*/ 
request.open("GET", option.url, true); 
//request.open("POST", option.url, true); 
// Set this property before sending the request , Gets the ready status on the server  
request.onreadystatechange = ResponseRequest; 
request.send(null); 
} 
/* 
request.readyState == 4 Indicates that the server has received 1 A response  
request.status == 200,HTTP The status value of the server response, denoted 1 Cut smoothly  
HTTP Ready state  
0 : request not issued (in call  open()  Before).  
1 : the request has been set up but not yet issued (call  send()  Before).  
2 : the request has been sent and is in the process of being processed (you can usually get the content header from the response here).  
3 : the request has been processed and some data is usually available in the response, but the server has not completed the response.  
4 : the response is complete, you can access the server response and use it.  
*/ 
var ResponseRequest = function() 
{ 
alert("HTTP Ready state:  "+request.readyState); 
if (request.readyState == 4) 
{ 
if (request.status == 200) 
{ 
alert("1 Cut smoothly !"); 
option.Success(request); 
} 
else 
{ 
alert(" An error has occurred. The error message is:  "+request.status); 
option.Failure(request); 
} 
} 
} 
sendRequest(); 
} 
// Determine whether the input value exists  
function getIS() 
{ 
/* 
1,url Is to link the page and pass the past value, pass the past value, let dynamic page execution  
2,Default.aspx For the page to be executed for this  
3,name Is the passed parameter name  
4,document.getElementById('Text1').value Pass the past parameter value  
5,message.responseText A message returned from the server after success  
*/ 
var option = 
{ 
url: "Default.aspx?name="+document.getElementById('Text1').value, 
Success:function(message) 
{ 
alert(message.responseText); 
} 
}; 
new ajax(option); 
} 

Aspx file
 
if (Request["name"] != null) 
{ 
this.Response.Clear(); 
string name = Request["name"].ToString(); 
if (name == "1") 
{ 
Response.Write(" The user name already exists, please fill in another user name !"); 
} 
else 
{ 
Response.Write(" This user name is not registered and can be used !"); 
} 

this.Response.End(); 
} 

2. Implemented by Jquery:
 
$(document).ready(function(){ 
$("#Button1").click(function(){ 
$.ajax({ 
type:"GET", 
url:"ResponsePage.aspx?name="+document.getElementById('Text1').value, 
success:function(message) { 
alert(message); 
} 

}); 

}); 
}); 

Related articles: