Let's say I have an Ajax call from jQuery like this:
$.ajax({
url: myUrl,
data: myData,
type:'post'
});
I would like to be able to add to the myData using
$.ajaxSetup({
beforeSend: function(call){...}
});
The result should be that all ajax calls (both post and get) is modified so if i get an extra parameter IsAjax=true
The beforeSend function is a pre-request callback function that runs before the request is sent to the server. The beforeSend() function use to set the custom headers and all, it is an Ajax event that triggers before an Ajax request is started.
One way to trigger an event is to use the jquery trigger function. Show activity on this post. function changeDate(){ $. ajax({ url, data: data, type: "POST", dataType: "json", success: function(cbdata) { update_table(cbdata); } }); } $('#selector').
processData. If set to false it stops jQuery processing any of the data. In other words if processData is false jQuery simply sends whatever you specify as data in an Ajax request without any attempt to modify it by encoding as a query string.
The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.
$.ajaxSetup({
beforeSend: function(jqXHR, settings) {
settings.data = $.extend(settings.data, {isAjax: true});
return true;
}
});
==== UPDATE ====
When
data
is an object, jQuery generates the data string from the object's key/value pairs unless theprocessData
option is set tofalse
. For example,{ a: "bc", d: "e,f" }
is converted to the string"a=bc&d=e%2Cf"
. If the value is an array, jQuery serializes multiple values with same key based on the value of thetraditional
setting (described below). For example,{ a: [1,2] }
becomes the string"a%5B%5D=1&a%5B%5D=2"
with the defaulttraditional: false
setting.
https://api.jquery.com/jquery.ajax/
As you can see, the setting processData should to be false. So it can be setted when you do request in $.ajax()
or globally in $.ajaxSetup()
.
This blog post explains how you can use $.ajaxSetup
to add data. It accumulates like $.extend
Just do this:
$.ajaxSetup({
data:{
isAjax:true
}
});
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