To reduce duplicated script, I created the following plugin, and it works as long as data
is left as is.
$.fn.myValid = function(rules, options) {
this.validate({
rules: rules.rules,
messages: rules.messages,
submitHandler: function(form) {
var o = Object.assign({},{
type:'POST',
url:null,
data: $(form).serializeArray(),
dataType: 'json',
success: function (rsp){
//Instead of reloading page, do dynamicly
$.unblockUI();
alert('record submitted')
location.reload();
},
error: function (xhr) {
$.unblockUI();
alert(xhr.responseJSON.message);
}
}, options);
//if (typeof o.data === "function") o.data=o.data(form);
$.blockUI();
$.ajax(o);
}
});
};
If data
needs to be changed, I cannot simply return something like $(form).serializeArray().concat($('#common-inputs').serializeArray(form))
because form
is not yet defined, so instead I thought returning a function would work. When ajax is fired, however, data
is a function, and not a string, array, or plain object, so data is not sent to the server.
$('#help-form').myValid(validObj.common, {url:'ajax.php','data': function(form){
console.log('this is never executed);
return $(form).serializeArray().concat($('#common-inputs').serializeArray(form))
}});
As a workaround, I included o.data=o.data(form);
(is commented out in above script) to execute the function to return results, and it works as desired. I expect, however, it is more of a hack, and there is a correct way to do this.
What is the proper way to use a function as data with jQuery ajax?
Will the solution be different for other properties such as url
, success
, etc?
Even with async: false , your function needs to return the return value of the $. ajax() call. Consider returning the actual observable object that you get from an async $. ajax() call and working with that, instead of running synchronous AJAX requests.
ajax({ async: true, contentType: 'application/json; charset=utf-8', type: "POST", dataType: 'json', data: JSON. stringify(arrays), url: "MyHandler. ashx", success: function (result) { b = true; }, error: function () { alert('Error occurred'); } });
I will use the jQuery. ajax() or $. ajax() method to call the C# method (WebMethod).
In the root of jQuery Ajax is ajax () function. This function is used to perform HTTP requests which are by default asynchronous. The syntax for using this function is: $.ajax ({name:value, name:value,...
JQuery is a great tool which provides a rich set of AJAX methods to develop next generation web application. This is very easy to load any static or dynamic data using JQuery AJAX. JQuery provides load () method to do the job − URL − The URL of the server-side resource to which the request is sent.
In the root of jQuery Ajax is ajax () function. This function is used to perform HTTP requests which are by default asynchronous. The syntax for using this function is: $.ajax ( {name:value, name:value, ... }) The parameters specifies one or more name/value pairs for the AJAX request. Possible names/values in the table below:
All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.
You can use beforeSend callback to set the data value:
beforeSend: function( xhr, settings ) {
this.data = $(form).serialize(); // or settings.data = $(form).serialize();
},
Same way you can modify url and success callbacks:
this.url = 'new-url';
this.success = newSuccessCallback;
Similar question
Note this does not work for GET requests, but POST should work just fine.
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