2013-10-04 78 views
0
<%@ 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 id="Head1" runat="server"> 
     <title>Login Page</title> 

     <script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script> 

    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div id="wholeContent"> 
      <div id="login"> 
       <table border="2"> 
        <tr> 
         <td> 
          User Id 
         </td> 
         <td> 
          <input type="text" id="txtId" name="txtId" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          Password 
         </td> 
         <td> 
          <input type="text" id="txtPwd" name="idPwd" /> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <input type="button" id="btnSubmit" value="Login" /> 
         </td> 
        </tr> 
       </table> 
      </div> 
      <div id="success"> 
      </div> 
      <div id="error"> 
      </div> 
     </div> 

     <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      $("#btnSubmit").click(function() 
      { 
        var uid = $("#txtId").val(); 
        var pwd = $("#txtPwd").val(); 
        var data=JSON.stringify({uid:uid,pwd:pwd}); 
        $.ajax({ 
        type: "POST", 

        url: "Default.aspx/ValidationWebMethod", 

        data: "{ 'uid': '" + uid + "','pwd': '"+pwd+"'}", 
        // data: data, 

        contentType: "application/json; charset=utf-8", 

        dataType: "json", 



        async: "false", 

        success: function (data) 
        { 

         alert(" Successful Login "+data.d); 
        // location.reload(); 

        }, 
        error: function(resp,textStatus,errorThrown) 
        { 
         alert("error : " + errorThrown + " || Check your Id and Password || "+ resp.d); 
        } 

        }); 
      }); 
     }); 
     </script> 

     </form> 
    </body> 
    </html> 

這是我的代碼隱藏(代碼的一部分)JQuery的AJAX方法給語法錯誤

[System.Web.Services.WebMethod] 
    public static void ValidationWebMethod(string uid, string pwd) 
    { 
     string text = uid + " : " + pwd; 

     System.IO.File.WriteAllText(@"D:\TrackLog.txt", text); 

     string[] flags = new string[1]; 

    // bool valid = true; 

     if (uid.Equals("arijit") && pwd.Equals("admin")) 
     { 
      flags[0] = "true"; 
      // return flags; 
     } 

     else 
     { 

      flags[0]="false"; 

     // return flags; 
     } 
    } 

現在,當我點擊該按鈕(btnSubmit按鈕)它給人的語法錯誤:無效字符(警告框中的errorThrown變量)..請告訴我爲什麼發生這種情況?

+0

您在警告框中收到了什麼錯誤? –

+0

我想再次檢查你的用戶名和密碼。是否給予寫入value.pls在這裏檢查:http://jsfiddle.net/ChYt3/1/ –

回答

0

的問題是在這裏

data: "{ 'uid': '" + uid + "','pwd': '"+pwd+"'}", 

正如你在JSON格式更改的數據與JSON.stringify

var strdata=JSON.stringify({uid:uid,pwd:pwd}); 

所以你沒有必要數據,如你在上面做傳遞給AJAX方法。

試試這個

data: strdata // renamed variable 

代替

data: "{ 'uid': '" + uid + "','pwd': '"+pwd+"'}", 
0

您ValidationWebMethod不返回任何東西。讓它返回,例如,字符串。