2

我在 asp.net 中使用 XMLHTTP AJAX

我正在向一个第一次失败的测试页面发出 ajax 请求,后来它会正常工作.. 为什么会发生这种情况?

XMLHTTP 请求 JS 代码

function test(Username)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    alert(xmlhttp.responseText);
    document.getElementById("UserStatus").innerHTML=xmlhttp.responseText;
    }
}
var url = "test.aspx?UserName=" + Username;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

test.aspx 页面代码

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Archana\Archu Work\Trainy\Pavan\Pavan\App_Data\Database.mdf;Integrated Security=True;User Instance=True");   
    SqlCommand cmd = null;
    protected void Page_Load(object sender, EventArgs e)
    {

            conn.Open();
            string username = Request.QueryString.Get("UserName");
            string str = "select Count(*) from Student where FirstName = '" + username + "'";
            cmd = new SqlCommand(str, conn);
            cmd.ExecuteNonQuery();
            int cnt = Convert.ToInt32(cmd.ExecuteScalar());
            string status = null;
            if (cnt == 1)
            {
                status = "UserName are Available";
            }
            else
            {
                status = "Plz Enter Other UserName";
            }
            Response.Write(status);
            conn.Close();
        }
4

0 回答 0