2013-03-23 33 views
0

以下是我的HTML代碼和C#code.the的是我不明白的事情工作HTML到ASP.net的通信是通過JSON

HTML代碼---

<!doctype html> 

    <html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <link rel="shortcut icon" type="img/ico" href="Images/favicon.ico"/> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script type="text/javascript"> 
     $("#btn_login").live("click", function() { 
     var GetLogDet = {}; 
     GetLogDet.username = document.getElementById('password').value; 
     GetLogDet.password = document.getElementById('password').value; 

     $.ajax({ 
      type: 'POST', 
      url: 'Login.cs/GetLogDet', 
      data: "{GetLogDet:" + JSON.stringify(GetLogDet) + "}", 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: function (r) { 
       alert(r.d.username); 
       alert(r.d.password); 
      } 
     }); 
    }); 
    </script> 
<script> 
$(function() { 
    $("#dialog").dialog(); 
    }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
<div id="dialog" title= " Login" > 
<div> 
    <table> 
    <tr> 
     <td><img src="Images/pics.gif"/></td> 
     <td><input type="text" id="username" name="uname"></td> 
    </tr> 

    <tr> 
     <td><img src="Images/pics2.gif" /></td> 
     <td><input type="password" id="password" name="pwrd"></td> 
    </tr> 
     <tr> 
     <td></td> 
     <td align="right"><input type="button" id="btn_login" class="login" value=">" onclick="searchKeyPress()"></td> 
    </tr> 
</table> 
    </div> 
    <div> 

    </div> 

    </div> 
    </form> 

    </body> 
    </html> 

和我的C#代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 


public partial class _Default : System.Web.UI.Page 
{ 
    [System.Web.Services.WebMethod] 
    public static Login GetLogDet(Login Logindet) 
    { 
     return Logindet; 
    } 
} 

public class Login 
{ 
    String username, password; 
    public String Username 
    { 
     get 
     { 
      return username; 
     } 
     set 
     { 
      username = value; 
     } 
    } 


    public String Password 
    { 
     get 
     { 
      return password; 
     } 
     set 
     { 
      password = value; 
     } 
    } 

    } 

回答

1

您需要將代碼包裝到document.ready()中。

,還可以使用.on()代替.live()因爲.live已被棄用。

$(function() 
{ 
    $("#dialog").dialog(); 

    $("#btn_login").on("click", function() 
    { 
     var GetLogDet = {}; 
     GetLogDet.username = document.getElementById('password').value; 
     GetLogDet.password = document.getElementById('password').value; 
     $.ajax(
     { 
      type: 'POST', 
      url: 'Login.cs/GetLogDet', 
      data: "{GetLogDet:" + JSON.stringify(GetLogDet) + "}", 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: function (r) 
      { 
       alert(r.d.username); 
       alert(r.d.password); 
      } 
     }); 
    }); 
}); 
0

btn_login爲界甚至在網頁完全加載點擊。應該使用document.ready事件來綁定這個按鈕。同樣如@dipesh所說,使用「on」而不是「live」