0

My ajax call is not going to server side. Here is my code:

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();
                }
            }
        }

Instead of going to the server side this error is raised:

The call to the server side failed. {“Message”:"Invalid object passed}

Carl Suster
  • 5,826
  • 2
  • 22
  • 36
Kamran
  • 371
  • 1
  • 4
  • 15

3 Answers3

2

the payload you are sending is not in the correct format.try

var userData = {'email': email ,
'pass': pass,
'firstName': firstName,
'lastName': lastName };
man_luck
  • 1,605
  • 2
  • 20
  • 39
  • It still doesn't work, it says "Message":"Invalid JSON primitive: email – Kamran Jul 17 '15 at 06:33
  • jquery might not be sending it as json data. instead it is sending it as a querystring. try data: JSON.stringify(userData) – man_luck Jul 17 '15 at 06:40
  • sorry sir it still generates same error after amending code – Kamran Jul 17 '15 at 06:49
  • not sure whats going on wrong here. Instead of using userdata in the ajax call, create the json object directly in the call as follows: data: Json.stringify({'email':email ,'pass':pass,'firstName':firstName,'lastName':lastName }) – man_luck Jul 17 '15 at 07:14
  • also see http://stackoverflow.com/questions/17873926/invalid-json-primitive-error and http://stackoverflow.com/questions/15293717/message-invalid-json-primitive-ajax-jquery-method-with-c-sharp-webmethod – man_luck Jul 17 '15 at 07:15
  • i had problem with payload... you were right man, but rectifying quotes helped the cause without changing the code... just thought to share with you :) cheers – Kamran Jul 20 '15 at 06:36
0

[Comment]: I can't write comments, so I write here. I did your scenario, but it didn't produce problem for me. Although when I tested userData with below link, It said "Strings should be wrapped in double quotes".

http://jsonformatter.curiousconcept.com

Try with double quotes like this:

    var userData = "{ \"email\":\"" + email +
        "\"  , \"pass\" : \"" + pass +
        "\"  , \"firstName\" : \"" + firstName +
        "\"  , \"lastName\" : \"" + lastName +
    "\" }";
Mohammad Eslami
  • 536
  • 1
  • 6
  • 17
0

You should not build a json string, json is a javascript object

var userData = {
    email: email,
    pass: pass,
    firstName: firstName,
    lastName: lastName
}
meda
  • 45,103
  • 14
  • 92
  • 122