给出语法错误的JQuery ajax方法

时间:2013-10-04 11:07:29

标签: c# jquery asp.net ajax

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

2 个答案:

答案 0 :(得分:0)

问题出在这里

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

当您使用JSON.stringify

更改json格式的数据时
var strdata=JSON.stringify({uid:uid,pwd:pwd});

所以你不需要像上面那样将数据传递给ajax方法。

试试这个

data: strdata // renamed variable 

而不是

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

答案 1 :(得分:0)

您的ValidationWebMethod不会返回任何内容。让它返回,例如,字符串。