I'm trying to replace a form submit with ajax call. the action needs formcollection and I don't want to create a new Model. So I need to pass the whole form (just like form submit) but through ajax call. I tried to serialize and using Json but the formcollection is empty. this is my action signature:
public ActionResult CompleteRegisteration(FormCollection formCollection)
and here is my submit button click:
var form = $("#onlineform").serialize();
$.ajax({
url: "/Register/CompleteRegisteration",
datatype: 'json',
data: JSON.stringify(form),
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.result == "Error") {
alert(data.message);
}
}
});
now how can I pass data into formcollection?
Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec. Now the function executes every 5 seconds and fetches new data from the server. It repeatedly executes the function even when the previous AJAX request is not successfully executed and return.
Since FormCollection
is a number of key-value pairs, JSON is inappropriate data format for its representation. You should use just serialized form string:
var form = $("#onlineform").serialize();
$.ajax({
type: 'POST',
url: "/Register/CompleteRegisteration",
data: form,
dataType: 'json',
success: function (data) {
if (data.result == "Error") {
alert(data.message);
}
}
});
Key changes:
Try:
$(<your form>).on('submit',function(){
$.ajax({
url: "/Register/CompleteRegisteration" + $(this).serialize(),
// place the serialized inputs in the ajax call
datatype: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.result == "Error") {
alert(data.message);
}
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With