Client page index.html
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Ajax test </title>
<Script language = "JavaScript" type = "text/JavaScript">
// Generate an HTTP request object for the client to send an asynchronous HTTP request to the service/end
Function gethttpobject ()
{
VaR HTTP;
VaR browser = navigator. appname;
If (Browser = "Microsoft Internet Explorer ")
{
// If you use IE, the ActiveX object of XMLHTTP is returned.
HTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
}
Else
{
// Otherwise, an XMLHTTPRequest object is returned.
HTTP = new XMLHttpRequest ();
}
Return HTTP;
}
// Obtain the global HTTP request object
VaR HTTP = gethttpobject ();
// Process Request status changes
Function gethello ()
{
// 4 indicates that the request has been completed
If (HTTP. readystate = 4)
{
// Obtain the response text of the Service segment
VaR hellostr = http. responsetext;
Alert (hellostr );
If (hellostr. charat (0 )! = "0 ")
{Alert ("the user name already exists! ");}
Else
{Alert ("the user name does not exist. It can be used! ");}
}
}
Function helloworld ()
{
VaR url = "check. aspx? Id = "+ document. getelementbyid (" text1 "). value;
// Specify the server address
HTTP. Open ("get", URL, true );
// Processing function when the Request status changes
HTTP. onreadystatechange = gethello;
// Send the request
HTTP. Send (null );
}
</SCRIPT>
</Head>
<Body>
<Input id = "text1" style = "width: pixel PX" type = "text"/>
<Input id = "button1" type = "button" value = "button" onclick = "javascript: helloworld ();"/>
</Body>
</Html>
Server Function page check. aspx
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class check: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = "select count (*) from table where name =" + request. querystring ["ID"]. tostring () + "";
Sqlconnection conn = new sqlconnection ("Data Source =.; initial catalog = database; Integrated Security = true ");
Conn. open ();
Sqlcommand cmd = new sqlcommand (STR, Conn );
Int I = (INT) cmd. executescalar ();
Conn. Close ();
Response. Write (I );
}
}
Transferred from: the sky released by relaxmyself