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>

这是我的代码隐藏(部分代码)

[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 变量)..请你告诉我为什么会这样?

4

2 回答 2

0

您的 ValidationWebMethod 不返回任何内容。使其返回,例如,字符串。

于 2013-10-04T11:27:27.627 回答
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+"'}",
于 2013-10-04T11:15:26.803 回答