Ajax簡單應用,檢測使用者名稱是否存在

來源:互聯網
上載者:User
用戶端頁面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測試</title>
<script language="javascript" type="text/javascript">
//產生Http請求對象,用於用戶端向服務/端發送非同步http請求
function getHttpObject()
{
var http;
var browser = navigator.appName;

if(browser == "Microsoft Internet Explorer")
{
//如果使用者使用IE,就返回XMLHTTP的ActiveX對象
http = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
else
{
//否則返回一個XMLHttpRequest對象
http = new XMLHttpRequest();
}

return http;
}
//擷取全域的HTTP請求對象
var http = getHttpObject();

//處理請求狀態變化
function getHello()
{
//4表示請求已完成
if (http.readyState == 4)
{

//擷取服務段的響應文本

var helloStr = http.responseText;
alert(helloStr);
if(helloStr.charAt(0)!="0")
{alert("使用者名稱已經存在!");}
else
{alert("使用者名稱不存在,可以實用!");}
}
}
function HelloWorld()
{
var url = "Check.aspx?id="+document.getElementById("Text1").value;

//指定服務端的地址
http.open("GET", url, true);
//請求狀態變化時的處理函數
http.onreadystatechange = getHello;
//發送請求
http.send(null);
}

</script>

</head>
<body>
<input id="Text1" style="width: 116px" type="text" />
<input id="Button1" type="button" value="button" onclick="javascript:HelloWorld();" />

</body>
</html>

伺服器端功能頁面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);
}
}

轉自:RelaxMyself 釋放的天空

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.