xmlHttp.responseText未知错误定义
xmlHttp.responseText Unknown Error define
我在html(使用xmlHttp.responseText)页面中创建了一个javascript,在该页面中,我从一个aspx页面请求一个值,该页面查询数据库(MSSQL)中用户名的用户号。当我加载html(IE8)时,我得到了"未知运行时错误行:30"。是什么导致了这个问题?需要帮助。这是我的代码:
-
这是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>
-
这是我的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();
}
相关文章:
- chrome.storage.sync未定义错误
- 为什么foodModule没有定义错误?Angular js
- 对象为null或IE9中存在未定义错误
- 是否排除节点中错误堆栈的第一行?/节点中的自定义错误类型
- axios promise如何定义错误
- 已定义变量出现未定义错误
- 'define' 在 RequireJS 和 Webapp Yo 生成器上未定义错误
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- onreadystatechange未定义错误
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 登录时显示自定义错误消息
- 如何修复JSlint报告的“foo未定义”错误
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 主干无法读取属性'属性'主干视图中的未定义错误
- 如何克服javascript中的未定义错误
- ReferenceError:事件未在Firefox中定义错误
- 当我在异步操作Redux上开始单元测试时,没有定义错误承诺
- require未定义错误
- AJAX自定义错误处理代码问题
- 单击链接时出现函数未定义错误