xmlHttp.responseText未知错误定义

xmlHttp.responseText Unknown Error define

本文关键字:定义 错误 未知 responseText xmlHttp      更新时间:2023-09-26

我在html(使用xmlHttp.responseText)页面中创建了一个javascript,在该页面中,我从一个aspx页面请求一个值,该页面查询数据库(MSSQL)中用户名的用户号。当我加载html(IE8)时,我得到了"未知运行时错误行:30"。是什么导致了这个问题?需要帮助。这是我的代码:

  1. 这是HTML页面和Javascript:

       <!DOCTYPE html>
       <html>
       <head>
       <script type="text/javascript">
       function showUsernum(str)
       {
          var xmlHttp;   
           if (str=="")
       {
        document.getElementById("textExists").innerHTML="";
        return;
        }
       if (window.xmlHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlHttp=new xmlHttpRequest();
        }
     else
        {// code for IE6, IE5
             xmlHttp=new ActiveXObject("Microsoft.xmlHttp");
        }
      xmlHttp.onreadystatechange=function()
       {
          if (xmlHttp.readyState==4 && xmlHttp.status==200)
          {
        //alert(str);
              document.getElementById("textExists").innerHTML=xmlHttp.responseText;
          }
       }
       xmlHttp.open("GET","http://localhost/Base_Data/default.aspx?q="+str,true);
       xmlHttp.send();
     }
     </script>
     </head>
     <body>
    <form action=""  method="post"> 
    <label>UserName
    <input type="text" name="textUser" id="textUser" onchange="showUsernum(this.value)">
    </label>
    </form>
    <br/>
    <div >
    <form name="form1" method="post" action="">
    <label>
    <div id="textExists">Exists?</div>
    </label>
    </form>
    </div>
    </body>
    

  2. 这是我的ASP代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        SqlConnection conn;
                string connection = ConfigurationManager.ConnectionStrings ["BaseData"].ConnectionString;
                conn = new SqlConnection(connection);
                string sql = "SELECT USERNUM FROM useraccount WHERE USERNAME ='" + Request.QueryString["q"] + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                string contNm = Convert.ToString(cmd.ExecuteScalar());
                Response.Write("textExists=" + contNm );
                conn.Close();
      }
    

真的很感激任何回应。非常感谢。

问题是,您试图将整个页面(包括<html>标记和所有内容)分配到一个DOM元素中,而IE并不喜欢这样。

要让服务器只发送原始HTML而不发送整个文档,您需要清除响应。此外,您没有正确处理数据库对象,并且暴露于SQL注入攻击,因此优化的代码将是:

string connection = ConfigurationManager.ConnectionStrings ["BaseData"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
    conn.Open();
    string sql = "SELECT USERNUM FROM useraccount WHERE USERNAME=@user";
    string contNm = "";
    using (SqlCommand cmd = new SqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@user", Request.QueryString["q"]);
        contNm = Convert.ToString(cmd.ExecuteScalar());
    }
    Response.Clear();
    Response.Write("textExists=" + contNm);
    Response.End();
    conn.Close();
}