I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing in my page. Each code block has three functions (code is incomplete and for illustrative purpose only):
encapsulates $.ajax
call:
function doSomething(data){
// do some preprocessing
$.ajax({}); // some JQuery Ajax operation that accepts data
// do some postprocessing
return false;
}
handles the response:
function handleResponse(result){
// process the result
return false;
}
handles any error:
function handleError(XMLHttpRequest, textStatus, errorThrown){
// gracefully handle the Error and present relevant information to user
return false;
}
In a page that requires a lot of data processing I end up having a lot of these blocks which seems to be duplication, so I decided to do some refactoring.
I figure there would be different ways to go about this.
I was just wondering if anyone has come across this and/or if there is a best practice solution for this?
The AJAX Toolkit uses SOAP API calls. SOAP calls always have a maximum limit of 50MB. However, it is also XML-based, which restricts the available characters you can use, so the file has to be Base-64 encoded. This puts the final limit at around 37 MB of binary data, as you've observed.
Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.
There are two different types of issues that can cause admin-ajax. php slow server responses. The first is a backend CPU issue and the second is more of a frontend issue where you will notice third party plugins polling this file in your website speed tests.
You can use the $.ajaxSetup({}) method in jQuery to setup some common ajax settings.
For instance, if you are going to be posting to the same URL over and over again on some page, you can just set that in the ajaxSetup. This would mean you would have to pass less parameters to a function like what Richard provided. Any property of the ajax method's first parameter can be set as a default in $.ajaxSetup().
$.ajaxSetup({
url: 'my/ajax/url'
success: function() {
// Do some default stuff on success
},
error: function() {
// Do some default stuff on a failure
}
// etc...
});
They can be overridden in any ajax call. So, now you can just do:
$.ajax({data:{foo:'bar',bar:'foo'}});
And you can override the URL, for instance like this:
$.ajax({url:'different/ajax/url',data:{foo:'bar',bar:'foo'}});
We have often used a wrapper function for the Ajax call in order to simplify the usage so you could do this:
function NewAjax(url, data, success)
{
$.ajax({
url: url,
data: data,
success: success,
fail: function ()
{
// Do generic failure handling here
}
}
But I often prefer to bind to every ajax event using the jQuery ajax events:
http://docs.jquery.com/Ajax
so you could bind to every failure or success of every ajax call such as:
ajaxError( callback ) ajaxSuccess( callback )
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