对服务器端的调用失败。 {“消息”:“传递的对象无效}

时间:2015-07-17 06:02:59

标签: jquery asp.net ajax

我的ajax调用不会转到服务器端。 这是我的代码:

function InsertData() {
    debugger;

    var email = $("#txtemail").val();
    var pass = $("#txtpass").val();
    var firstName = $("#txtfirstName").val();
    var lastName = $("#txtlastName").val();
    var userData = "{'email':'" + email + "','pass':'" + pass + "'firstName':'" + firstName + "','lastName':'" + lastName + "'}";
    $.ajax({
        type: "POST",
        url: "Home.aspx/ExecuteInsert",
        data: userData,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg);
        },

        error: function (x, e) {   
            alert("The call to the server side failed. " + x.responseText);
        }
    });

}

C#

 [WebMethod]
        public static void ExecuteInsert(string email, string pass, string firstName, string lastName)
        {
            string connStr = @"Data Source=userinfo; Database= MYWEB;User ID=myweb;Password=***********";
            using (SqlConnection connect = new SqlConnection(connStr))
            {
                using (SqlCommand command = new SqlCommand("web_proc_testweb_demo" , connect))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(@email, SqlDbType.VarChar).Value = pass;
                    command.Parameters.Add(@pass, SqlDbType.VarChar).Value = firstName;
                    command.Parameters.Add(@firstName, SqlDbType.VarChar).Value = lastName;
                    command.Parameters.Add(@lastName, SqlDbType.VarChar).Value = email;
                    command.ExecuteNonQuery();
                }
            }
        }

而不是转到服务器端,而是引发此错误:

  

对服务器端的调用失败。 {“消息”:“传递的对象无效}

3 个答案:

答案 0 :(得分:2)

您发送的有效负载格式不正确。\ n \ t

{{1}}

答案 1 :(得分:0)

[评论]:我不能写评论,所以我在这里写。 我做了你的场景,但它并没有给我带来麻烦。虽然当我用下面的链接测试userData时,它说"字符串应该用双引号括起来"。

http://jsonformatter.curiousconcept.com

尝试使用双引号:

    var userData = "{ \"email\":\"" + email +
        "\"  , \"pass\" : \"" + pass +
        "\"  , \"firstName\" : \"" + firstName +
        "\"  , \"lastName\" : \"" + lastName +
    "\" }";

答案 2 :(得分:0)

你不应该构建一个json字符串,json是一个javascript对象

var userData = {
    email: email,
    pass: pass,
    firstName: firstName,
    lastName: lastName
}