2011-12-09 60 views
0

我是Jquery Ajax的新手。我需要你的幫助。我想檢查我的數據庫中的Username Exist,並在span元素旁邊顯示文本。我已經做了一些參考話題,但我不能解析它無效的Web服務調用,缺少JQuery和asp.net參數的值

{"Message":"Invalid web service call, missing value for parameter: \u0027username\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary 2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target,IDictionary 2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary 2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} *

在ASPX

<asp:TextBox ID="txtusername" runat="Server" name="username" class="inputbox frontlogin"/><span id="checkReturn"></span> 

在後面的代碼

[System.Web.Services.WebMethod] 
public static bool KiemTraLogin(string username) 
{ 
    CommonData dt = new CommonData(); 
    bool check = false; 
    DataTable tb = dt.TruyVanTheoTenDangNhap(username); 
    if (tb.Rows.Count > 0) 
    { 
     check = false; 
    } 
    else 
     check = true; 
    return check; 
} 

在JavaScript

<script type="text/javascript" language="javascript" src="Scripts/jquery-1.2.6.min.js"></script> 
<script type="text/javascript" language="javascript"> 
    var $j = jQuery.noConflict(); 
    $j(document).ready(function() { 
     $j("#<%=txtusername.ClientID %>").blur(function() { 
      $j.ajax({ 
       type: "POST", 
       url: "Register.aspx/KiemTraLogin", 
       data: "{'Username':'" + $j("#<%=txtusername.ClientID %>").val() + "'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (message) { 
        if (message.d == false) { 
         $j("#checkReturn").css({ 
          "color": "red", 
          "font-weight": "bold", 
          "font-size": "small", 
          "padding-left": "5px" 
         }); 
         $j("#checkReturn").text("Username Exist!"); 

         $j("#btnSubmit").hide(); 
        } 
        else { 
         $j("#checkReturn").css({ 
          "color": "green", 
          "font-weight": "bold", 
          "font-size": "small", 
          "padding-left": "5px" 
         }); 
         $j("#btnInsert").show(); 
         $j("#checkReturn").text("UserName OK") 
        } 
       }, 
       error: function (errormessage) { 

        $j("#checkReturn").text(errormessage.responseText); 
       } 
      }); 
     }); 
    }); 
</script> 

存儲過程

ALTER Procedure [dbo].[spTB_TruyVanTheoTenDangNhap] @TenDangNhap nvarchar(50)as select Username from Accinfo where Username = @TenDangNhap 

Linq的代碼

public DataTable TruyVanTheoTenDangNhap(string TenDangNhap) 
{ 
    DataTable dt = new DataTable(); 

    try 
    { 
     DataSet ds = new DataSet(); 
     SqlCommand cmd = new SqlCommand("spTB_TruyVanTheoTenDangNhap"); 
     cmd.Parameters.Add(new SqlParameter("@TenDangNhap", SqlDbType.NVarChar)); 
     cmd.Parameters[0].Value = TenDangNhap; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     da.Fill(ds); 
     dt = ds.Tables[0]; 

    } 
    catch (System.Exception ex) 
    { 

    } 

    return dt; 
} 

提前感謝!

+0

JavaScript嵌入到頁面中或作爲資源加載?對#<%= something.ClientID%>的調用可能不起作用 – Alfabravo

+0

對於你的數據字符串,只是做「{Username:Dude}」而不是jquery,看看能否解決你的錯誤。 –

回答

0

你可以聲明一個局部變量來保存你的JSON對象。然後使用瀏覽器的開發人員工具來設置中斷點並逐步瀏覽您的JQuery。然後檢查一下你的JQuery選擇器返回的結果。

var userModel = { Username: $j("#<%=txtusername.ClientID %>").val() }; 
    $j.ajax({ 
     type: "POST", 
     url: "Register.aspx/KiemTraLogin", 
     data: userModel, 
     contentType: "application/json; charset=utf-8", 
相關問題