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?
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?
Share Improve this question edited Jan 15, 2014 at 11:34 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Jan 15, 2014 at 11:31 user3189357user3189357 1531 gold badge6 silver badges17 bronze badges 4-
where is your ajax call...
.submit()
is notajax()
call BTW – bipen 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 – andrew Commented Jan 15, 2014 at 11:35
- api.jquery./jquery.ajax – MrUpsidown Commented Jan 15, 2014 at 11:36
- @user3189357 reply from judder is perfect. Read the api.jquery./jquery.ajax page for more info on ajax paramenters – andrew Commented Jan 16, 2014 at 11:58
2 Answers
Reset to default 3A 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./jquery.post/ for information on handling callbacks.
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;
});