Jquery做ajax在asp.net中

在不同浏览器中创建ajax对象

            function createXmlHttp() {//创建xhr对象

                varxhobj = false;

                try {

                    xhobj = new ActiveXObject("Msxml2.XMLHTTP"); // ie msxml3.0+

                } catch (e) {

                    try {

                        xhobj = new ActiveXObject("Microsoft.XMLHTTP"); //ie msxml2.6

                    } catch (e2) {

                        xhobj = false;

                    }

                }

                if (!xhobj && typeofXMLHttpRequest != 'undefined') {// Firefox, Opera 8.0+, Safari

                    xhobj = new XMLHttpRequest();

                }

                return xhobj;

            }

 从做通过ajax实现后台验证用户名,想起就悲催,今天机试有点感冒环境一变有点不适应大脑一片空白做以前的功能居然写不出来。。。反省中

前台

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>注册用户</title>
<style type="text/css">
.hid{ display:none;}
.errmsg
{
background-color: Red;
font-weight: bold;
color: White;
}
</style>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#UserName").blur(function () {
reg = /^[a-zA-Z]{1,30}$/;
if ($("#UserName") == "" || !reg.test($("#UserName").val())) {
$("#Errmsg").attr("class", "errmsg");
$("#imgMsg").attr("style","display:none;");
return;
}
$("#Errmsg").attr("class", "hid");
$.post("CheckUser.ashx", { "loginId": $(this).val() },
function (data) {//回调函数中
if (data == "yes") {
//用户已经存在
$("#imgMsg").attr("src", "images/cha.ico").show();
}
else {
$("#imgMsg").attr("src", "images/dui.ico").show();
}
}
)
})
});
function Validate() {
if ($("#Errmsg").attr("class") == "errmsg" || $("#UserName").val()==""||$("#imgMsg").attr("src") == "images/cha.ico") {
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td colspan="3">注册用户</td></tr>
<tr><td>用户名:</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
</td>
<td>
<span id="Errmsg" class="hid">不能超过30,且为英文字母</span><img id="imgMsg" src="" style="display:none;"/>
</td></tr>
<tr><td>密码:</td>
<td>
<asp:TextBox ID="UserPwd1" runat="server"></asp:TextBox>
</td>
<td>

</td></tr>
<tr><td>重复密码:</td>
<td>
<asp:TextBox ID="UserPwd2" runat="server"></asp:TextBox>
</td>
<td>

</td></tr>
<tr><td colspan="3">
<asp:Button ID="Register" runat="server" OnClientClick="return Validate()" Text="Button"/></td></tr>
</table>
</div>
</form>
</body>
</html>

ashx

 

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string loginName = context.Request.Form["loginId"].ToString();
BLL.TUsersBLL bll = new BLL.TUsersBLL();
bool bl = bll.SelectUser(loginName);
if (bl)
{
context.Response.Write("yes");
}
else
{
context.Response.Write("no");
}
}

总结:

由于太过于依赖编译器自动提示导致笔试有些悲哀,机试对于环境不熟,心急导致错误连连。。。

工作中千万别害羞,可能在遇到问题时走进一根筋,也许需要另一只手拉你一下。。。相互学习促进成长。。。。。

posted @ 2013-04-06 00:40  ~峰~  阅读(212)  评论(0编辑  收藏  举报