我想用Jquery+WebService 实现用户名校验
但我点击“校验”既然没有反应  $("#btnCheck").click(function() {}不是取到“校验”了么------
Default.aspx 界面<%@ 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>
    <link href="css/Sy_User.css" type="text/css" rel="Stylesheet"/>
    <script src="Jquery/jquery.js" type="text/javascript"></script>
    <script src="Jquery/UserInfo.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         用户名:<input id="txtUserName" type="text" class="txtUserName"/>
        <input id="btnCheck" type="button" value="检验" /></div>
         <div id="result"></div>   
    </form>
</body>
</html>
------------
css 
  ---Sy_User.css    .txtUserName 
{
   border:1px solid red;
     
}Js
-----------
    --Jqueyr.js 我就不写出来了
    --UserInfo.js      $(document).ready(function() {
    /*
    获取按钮*/
    userName = $("#txtUserName");
    $("#btnCheck").click(function() {      var txtUserName = userName.val();
        if (txtUserName == "") {
            alert("用户名不能为空");
        }
        else 
        {
            $.get("http:127.0.0.1/JqueryUser/WebService.asmx/checkUserName?uName=" + txtUserName, null, function(uName) {
                $("#result").html(uName);
            });
        }
    });    userName.keyup(function() {    var value = userName.val();
        if (value == "") {
            userName.addClass("txtUserName");
        }
        else {
            userName.removeClass("txtUserName");
        }
    });});----------------
  WebService.asmx
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {    public WebService () {        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string checkUserName(string  uName)
    {
        SqlConnection con = new SqlConnection( ConfigurationManager.ConnectionStrings["strCon"].ToString());
        con.Open();
        string sql = "select UName from UserInfo where UName='" + uName + "' ";
        SqlCommand comm = new SqlCommand(sql,con);
        SqlDataReader dr = comm.ExecuteReader();
        if (dr.Read())
        {
            return "用户名不可以用";
        }
        else
        {
           return "用户名可以用";
        }         
    }
}----------

解决方案 »

  1.   

    LZ的问题不懂的 不过最近才找到的一个比较好看的jquery效果 在线演示 LZ可以看一下
    在线演示jquery
      

  2.   

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]//设置为Get请求。(建议楼主设置成POST,UseHttpGet = true)
      public string checkUserName(string uName)
      {
    //.....
     if (dr.Read())
      {
      return "用户名不可以用";
      }
      else
      {
      return "用户名可以用";
      }
    }
    你这样return出去的是xml格式的
      

  3.   

    第一:先检查WS是否正常运行了。
    第二:要在web.config中添加HttpGet和HttpPost或Soap协议。
    第三:在js中发出请求的时候,加上请求头:SOAPAction
    第四:WebService是通过xml来传输数据的,因此,它返回的也是xml格式的数据。而且这个格式在Get和Post下是不一样的,使用Get的时候,返回的格式简单些。而使用Post的话,在发出请求的时候,还需要自己组建Soap数据。
    ----
    我想楼主的问题多半是没有在web.config中添加协议。