Ori Refael
Ori Refael

Reputation: 3008

cant send data from jquery to c# server

been trying to make a jquery validatation for correct input and server validation to check if user/email exists. only problem is that the parameters are not always sent from the client side for some reason even after this passes the jquery validation. tried enter breaking point in the beginning of the server side method, rarely enters there, placed alert after inserting data to params and sees the json looks OK. im using Jquery, asp.net, c#.

client code:

var user = $("#txt_register_username").val();
            var password = $("#txt_register_password").val();
            var firstname = $("#txt_register_firstname").val();
            var lastname = $("#txt_register_lastname").val();
            var birthdate = $("#txt_register_birthdate").val();
            var picture = $("#txt_register_picture").val();
            var car = $("#txt_register_car").val();
            var email = $("#txt_register_email").val();
            var home = $("#txt_register_home").val();
            var cell = $("#txt_register_cell").val();
            var params = "{username:" + user
            + ",firstname:" + firstname
            + ",lastname:" + lastname
            + ",birthdate:" + birthdate
            + ",pic:123"
            + ",carowned:" + car
            + ",email:" + email
            + ",password:" + password
            + ",home:" + home
            + ",cell:" + cell
            + "}";
            $.ajax
            ({
                type: "POST",
                url: "Register.aspx/AddUser",
                data: params,
                contentType: "application/json; charset=utf-8",
                dateType: "json",
                success: function (result) {
                    alert(result.d);

                }
            });

the pic will later be binary, so its just temporary, ignore it for being string.

server code:

[WebMethod]
        public static string AddUser(string username, string firstname, string lastname, string birthdate, string pic, string carowned, string email, string password, string home, string cell)
        {
            DataSet1TableAdapters.UsersTableAdapter userAdapter = new DataSet1TableAdapters.UsersTableAdapter();
            DataSet1.UsersDataTable userTable = new DataSet1.UsersDataTable();
            userAdapter.Fill(userTable);
            int index = 0;
            Console.WriteLine("entering valiation area");
            foreach (DataRow dr in userTable.Rows)
            {
                if (userTable.Rows[index]["username"].Equals(username))
                {
                    Console.WriteLine("a user who already exists tried to create an account");
                    return "user already exists with that username";        
                }
                if (userTable.Rows[index]["email"].Equals(email))
                {
                    Console.WriteLine("a user who already exists with an email tried to create an account");
                    return "user already exists with that email";
                }
                index++;
            }
            try
            {
                userAdapter.Insert(username, firstname, lastname, birthdate, pic, carowned, email, password, home, cell);
                Console.WriteLine("new user added to db");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return "oops , something went wrong.";
            }
            return "new user added to database.";

        }

Upvotes: 0

Views: 1028

Answers (5)

scaifyuk
scaifyuk

Reputation: 23

As Jeremy mentioned your JSON is incorrect

it needs to be something like this {"username" : "user", "firstname" : "firstname" .... }

download fiddler and you will be able to see what issue you are getting, you will then be able to take the posted json and run it through JSONLint.com which will tell you why your json is formed incorrectly.

Also get json2.js and then you can do what dotnet dreamer has shown you, stringify may be built into some of the newer browsers, think firefox has it but older browsers like ie8 and below don't, it may now be built into jquery so I would check first, haven't done much ajax stuff lately

Upvotes: 0

Idrees Khan
Idrees Khan

Reputation: 7752

try;

var params = JSON.stringify({
             username:user,
            firstname :firstname,
            lastname:lastname,
            birthdate: birthdate,
            pic:123,
            carowned:car,
            email:email,
            password: password,
            home: home,
            cell: cell
           });

Upvotes: 4

Chris Dixon
Chris Dixon

Reputation: 9167

It's due to your AJAX call being a "POST", rather than "GET", so the parameters are treated differently.

You need something like:

var data = $.toJSON(params);

then send the data object with the AJAX request, rather than params.

Upvotes: 0

Ali Bahraminezhad
Ali Bahraminezhad

Reputation: 326

You should send your data in json object but you are sending it as a string! Switch your syntax to something like this:

var params = {username: + user
            ,firstname: + firstname
            ,lastname: + lastname
            ,birthdate: + birthdate
            ,pic:123
            ,carowned: + car
            ,email: + email
            ,password: + password
            ,home: + home
            ,cell: + cell
            };

Edit: If I were you I would remove dataType property to see how it works!

Upvotes: 0

user1932079
user1932079

Reputation:

Try switching to this syntax:

var params = {"username" : user,
              "firstname" : firstname
              ....
             };

jQuery's data expects a json object to parse into server variables.

Upvotes: 1

Related Questions