3
function ajax() {
  $('form').submit(function() {
    console.log($(this).serializeArray());
    $('#result').text(JSON.stringify($(this).serializeArray()));
    return false;
  });
}

This is the json data i'm getting:

[{"name":"firstName","value":"erere"},{"name":"lastName","value":"rere"},{"name":"emailAddress","value":"[email protected]"},{"name":"password","value":"dfdfd"},{"name":"phoneNumber","value":"989989898"}]

How can I send it to the server. What should I include in data in the ajax call?

4
  • where is your ajax call... .submit() is not ajax() call BTW Commented Jan 15, 2014 at 11:33
  • send it like you have them. If you are using php you can json_decode() the data server-side. Also, you can use $(form).serialize() to get a simple name:value pair to submit to your proccess script Commented Jan 15, 2014 at 11:35
  • api.jquery.com/jquery.ajax Commented Jan 15, 2014 at 11:36
  • @user3189357 reply from judder is perfect. Read the api.jquery.com/jquery.ajax page for more info on ajax paramenters Commented Jan 16, 2014 at 11:58

2 Answers 2

3

A simple example:

$('form').submit(function() {

    $.post( "send.php", $(this).serializeArray())
      .done(function( reply ) {
        alert( "Complete, reply from server: " + reply );
      });

    return false;
});

See: http://api.jquery.com/jquery.post/ for information on handling callbacks.

Sign up to request clarification or add additional context in comments.

2 Comments

It's whatever you have server side to process the form.
I wish you could downvote comments, you'll need to give us more information to explain what is wrong. Perhaps an error message, or what line the error is occurring on? As far as I am aware there is no 555 error code in HTTP, are you talking about an email error?
2

Try this:

$('form').submit(function() {
    var form = $(this);
    var data = form.serialize();

    $.ajax({
        url: 'post url'
        method: 'POST',
        data: data,
        success: function(resp){
             //action on successful post
        },
        error: function() {
            //handle error
        }
    });
    return false;
});

2 Comments

It looks like there's an issue with jQuery serializing your form data, try changing the form.serialize() to form.serializeArray()
@aRahmanS29 it'd be better to ask @judder first if he meant to use the .serializeArray method instead and not just edit it assuming this is true because it might not have been his intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.