Jquery ajax响应是空白的

时间:2014-05-26 06:58:26

标签: jquery asp.net

我正在使用jquery ajax方法,但响应在弹出窗口中显示为空白。如果我在成功区域中警告静态消息,然后显示我的消息,如果我尝试提醒响应,则数据显示为空白。

请帮忙

[WebMethod, ScriptMethod(UseHttpGet = true)]
public static string InsertData(string MainArr)
{
    string sSuccess=string.Empty;
    JavaScriptSerializer JSS=new JavaScriptSerializer();
    propertyClass[] arr=JSS.Deserialize<propertyClass[]>( MainArr);
    if (arr.Length > 0)
    {
        sSuccess = "SUCCESS";
    }
    else
    {
        sSuccess = "No";
    }
    return sSuccess;
} 

 public class propertyClass
 {
   public string UserName    { get; set; }
   public string Password    { get; set; }
   public string EmailId    { get; set; }
   public string Mobile    { get; set; }

}

Jquery的

  $('[id$=submitdata]').click(function () {
            //Set Data in variable
            var MainArr = [];
            var vUserName = $('[id$=txtUserName]').val();
            var vPassword = $('[id$=txtPassword]').val();
            var vEmailId = $('[id$=txtEmail]').val();
            var vMobile = $('[id$=txtMobile]').val();

            //Push data in array
            MainArr.push({
                UserName: vUserName,
                Password: vPassword,
                EmailId: vEmailId,
                Mobile: vMobile
            });

            //Insert data
            $.ajax({
                type: "Post",
                url: "Validation.aspx/InsertData",
                data: JSON.stringify(MainArr),
                cache: false,
                success: function (data) {                        
                    alert(data);
                },
                error: function () { alert('error') }

            });

            return false;
        });

1 个答案:

答案 0 :(得分:0)

也许它会帮助你:

方法1

客户端代码 -

 $('[id$=submitdata]').click(function () {
        //Set Data in variable
        var MainArr = [];
        var vUserName = $('[id$=txtUserName]').val();
        var vPassword = $('[id$=txtPassword]').val();
        var vEmailId = $('[id$=txtEmail]').val();
        var vMobile = $('[id$=txtMobile]').val();

        //Push data in array
        MainArr.push({
            UserName: vUserName,
            Password: vPassword,
            EmailId: vEmailId,
            Mobile: vMobile
        });

        //Insert data
        $.ajax({
            type: "Post",
            url: "Validation.aspx/InsertData",
            data: JSON.stringify({ myArr: MainArr }),
            cache: false,
            success: function (data) {
                alert(data);
            },
            error: function () { alert('error') }

        });

        return false;
    });

服务器端代码 -

[System.Web.Services.WebMethod]
        public static string InsertData(List<string> myArr)
        {
            string sSuccess = string.Empty;
            JavaScriptSerializer JSS = new JavaScriptSerializer();
            propertyClass[] arr = JSS.Deserialize<propertyClass[]>(myArr[0].ToString());
            if (arr.Length > 0)
            {
                sSuccess = "SUCCESS";
            }
            else
            {
                sSuccess = "No";
            }
            return sSuccess;
        }

方法2

客户端代码 -

$('[id$=submitdata]').click(function () {
        //Set Data in variable
        var MainArr = [];
        var vUserName = $('[id$=txtUserName]').val();
        var vPassword = $('[id$=txtPassword]').val();
        var vEmailId = $('[id$=txtEmail]').val();
        var vMobile = $('[id$=txtMobile]').val();

        //Push data in array
        MainArr.push({
            UserName: vUserName,
            Password: vPassword,
            EmailId: vEmailId,
            Mobile: vMobile
        });

        //Insert data
        $.ajax({
            type: "Post",
            url: "Validation.aspx/InsertData",
            data: "{'myArr':" + JSON.stringify(MainArr) + "}",
            cache: false,
            success: function (data) {
                alert(data);
            },
            error: function () { alert('error') }

        });

        return false;
    });

服务器端代码 -

[System.Web.Services.WebMethod]
        public static string InsertData(List<string> myArr)
        {
            string sSuccess = string.Empty;
            JavaScriptSerializer JSS = new JavaScriptSerializer();
            propertyClass[] arr = JSS.Deserialize<propertyClass[]>(myArr[0].ToString());
            if (arr.Length > 0)
            {
                sSuccess = "SUCCESS";
            }
            else
            {
                sSuccess = "No";
            }
            return sSuccess;
        }