我用Ajax和Webservice 实现一个判断用户名是否存在的功能
前台页面我是这也写的
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>无标题页</title>
<script src="JS/JScript.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<input id="txtName" type="text" runat="server" onblur="startRequest();" />
<div id="div1"></div>
</div>
</form>
</body>
</html>在JS文件JScript.js中我是这样写的var xmlHttp;
function createXMLHttpRequests()
{
if(window.ActiveXObject) //判断是否是IE浏览器
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else
{
alert("该浏览器不支持AJAX,请更换浏览器");
}}function startRequest()
{
createXMLHttpRequests();
var name=document.getElementById("txtName").value;
alert(name);
xmlHttp.open("GET","WebService.asmx/CheckUserName?name="+name,true);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("div1").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.send(null);
}webservice里面的方法我是这样写的:
[WebMethod]
public string CheckUserName(string name)
{
string ss = "";
string sql = "select * from Business where BusiName='"+name+"'";
SqlConnection conn = new SqlConnection(DBHelper.connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
object dd = cmd.ExecuteScalar();
int i = 0;
if (dd != null)
{
i = int.Parse(dd.ToString());
} conn.Close();
if (i != 0)
{
ss = "该用户名可以使用"; }
else
{
ss = "不能使用"; }
return ss;
}当鼠标移开文本框时判断是否有该用户名我单独测试了webservice完全可以,但是当我用ajax调用的时候,我输入的用户名在数据库中存在,可老是显示:不能使用。。
可能是调用参数的问题,我始终找不到 原因,哪位大哥大姐帮帮忙啊!
前台页面我是这也写的
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>无标题页</title>
<script src="JS/JScript.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<input id="txtName" type="text" runat="server" onblur="startRequest();" />
<div id="div1"></div>
</div>
</form>
</body>
</html>在JS文件JScript.js中我是这样写的var xmlHttp;
function createXMLHttpRequests()
{
if(window.ActiveXObject) //判断是否是IE浏览器
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else
{
alert("该浏览器不支持AJAX,请更换浏览器");
}}function startRequest()
{
createXMLHttpRequests();
var name=document.getElementById("txtName").value;
alert(name);
xmlHttp.open("GET","WebService.asmx/CheckUserName?name="+name,true);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("div1").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.send(null);
}webservice里面的方法我是这样写的:
[WebMethod]
public string CheckUserName(string name)
{
string ss = "";
string sql = "select * from Business where BusiName='"+name+"'";
SqlConnection conn = new SqlConnection(DBHelper.connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
object dd = cmd.ExecuteScalar();
int i = 0;
if (dd != null)
{
i = int.Parse(dd.ToString());
} conn.Close();
if (i != 0)
{
ss = "该用户名可以使用"; }
else
{
ss = "不能使用"; }
return ss;
}当鼠标移开文本框时判断是否有该用户名我单独测试了webservice完全可以,但是当我用ajax调用的时候,我输入的用户名在数据库中存在,可老是显示:不能使用。。
可能是调用参数的问题,我始终找不到 原因,哪位大哥大姐帮帮忙啊!
你这个 需要托一个scriptmanage 控件~~~
url问题
1
var name=document.getElementById("txtName").value;
这句,最好加上url编码 var name=escape(document.getElementById("txtName").value);
在webservice端用Server.UrlDecode解码,这样可以保证中文用户名不出错。2
string sql = "select * from Business where BusiName='"+name+"'";
这句绝对会被注入,用sql参数查询吧。
System.Web.HttpUtility.UrlEncode(NAME)