Cancel jQuery ajax error in .fail() function - javascript

I'm working on a site that has a global Ajax error handler in the main page template, like this:
$(document).ajaxError(function (event, request, settings, thrownError) {
postError(thrownError);
})
This updates the page with information about an error from an Ajax request. I am working on a specific Ajax call where I need errors to be handled differently, it will just silently select a value without doing anything about the error. The function is in a handler for a DevExtreme SelectBox that must return a Promise:
return $.ajax({
// stuff
}).fail(function (request, status, error) {
// set value
});
This does what its supposed to in regards to setting the value, but the Ajax error handler still fires. I've tried setting the error to null in the .fail() function but it still gets posted. Is there a way to disable the ajaxError handler for just this one call?

Seems like you want to prevent bubbling the error up for a particular case. See the following post and I believe this is what you are looking for:
Javascript: How to stop multiple jQuery ajax error handlers?
Hope this helps!

Related

How to handle server responses in dojo/store/JsonRest?

When using 'add' or 'put' on a dojo/store/JsonRest store, I would like to have an event handler for the server response, either on error or success.
I have tried adding an option like error: function() {} to the 'add' call, but this does not get triggered. Is there a special event or option that deals with the server response?
OK, so the answer is to use then, as it seems the add method returns a Deferred.
So you can do: store.add(...).then(function(value){ /* success */ }, function(error){ /* error */ })

Abort Wicket Ajax event on 'before' event subscription

I know that I can subscribe to events using Wicket 6+ which is what I do in my application. Now I am trying to cancel an Ajax event on a particular occurrence of some condition like that:
Wicket.Event.subscribe('/ajax/call/before', function (jqEvent,
attributes,
jqXHR,
errorThrown,
textStatus) {
if(someCondition) {
// Abort event, but how?
}
});
I am looking for a way to abort the event, but the normal jQuery event handlers
jqEvent.stopImmediatePropagation();
jqEvent.preventDefault();
or even
attributes.event.stopImmediatePropagation();
attributes.event.preventDefault();
but they do not seem to work. If the method returns a value, this does not seem to have an effect either. The easiest I found so far is simply throwing an exception but this solution is far from clean.
It is rather hacky but by following down the stack I found a solution to this. Wicket treats the component's callback handlers privileged. If you are registering an IAjaxCallListener, it will be allowed to return false which is read by Wicket and what stops the propagation. The "global" event listeners are for some reason denied this privilege which is why the return value must be smuggeled into an not-yet existing array of precondition handlers. It's messy but it works.
Wicket.Event.subscribe('/ajax/call/before', function (jqEvent,
attributes,
jqXHR,
errorThrown,
textStatus) {
if (precondition) {
attributes.pre = [stopWicket];
}
}
});
function stopWicket() {
return false;
}
Anyways. who calls something a "precondition" but ignores the decision made on this precondition handler....
A subscriber cannot stop Ajax processing.
Why not use a precondition for this? See AjaxRequestAttributes#getAjaxCallListeners() and IAjaxCallListener#getPrecondition().

How to manully trigger an ajaxComplete event

Using Jquery I need to trigger a ajaxComplete event.
At the moment I'm using this code with no success
$.getJSON(assetUrl, function (data) {
...
$.trigger("ajaxComplete");
With Error:
TypeError: 'undefined' is not a function (evaluating '$.trigger("ajaxComplete")')
Any idea what I'm doing wrong? Thanks
The ajaxCompleted event is fired on the DOM, and you will need to call the trigger method on a jQuery wrapper element: $(document).trigger(...), for example.
There is not static function "trigger" on the jQuery object (that's what the error message is telling you), you might use $.event.trigger - though I fear that's internal.
However, you won't need to do it manually; getJSON does trigger the event itself. For aborting a running ajax request, see the abort method of XHR objects.
You can define a global jQuery ajaxComplete (and ajaxError) function that will run on document ready and after every completed ajax request. You can define the ajaxComplete function on the intial page load (or whenever really) like this:
$(function(){
$(document).ajaxComplete(function(){
// on complete code
}).ajaxError(function(){
// on error code
});
});
To call this event handler at any time, just execute the following:
$(document).triggerHandler('ajaxComplete');
If anybody else is looking at this, the correct way to manually trigger ajaxComplete is $(document).trigger('ajaxComplete', [xhr, settings]);
It's probably important to pass the xhr object to the ajaxComplete trigger event, as the event handler might need it.
However, you only need this, if you're not making your requests through jquery, since jquery handles this automatically for you.

Override Ajax Success event

I am trying to override the jQuery ajax function to handle a default action on a success event but also executing the callback function that i am using in the options parameter.
What the purpose is there is tags returning in the response that I always want to strip out of the response for use elsewhere.
The scenario is:
Ajax submit
Ajax Success
--DEFAULT SUCCESS ACTION
--Call Ajax Success Callback
Can anyone help?
I have tried extending
jQuery.ajax
jQuery.ajaxSuccess
jQuery.ajax.done
The code I have is:
var _ajaxSuccess = jQuery.fn.ajaxSuccess;
$.fn.extend({
ajaxSuccess: function (a)
{
_ajaxSuccess.apply(this, a);
}
});
There is the global ajaxSuccess callback:
Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the .ajaxSuccess() method are executed at this time.
That will let you call your own function on every successful AJAX call without interfering with the usual success callbacks.
There are various other global AJAX event handlers that you might want to look at too.
If those callbacks don't have the right timing or capabilities for you, then you could write your own wrapper for $.ajax and use that:
function wrapped_ajax(options) {
var success = options.success;
options.success = function(data, textStatus, jqXHR) {
// Do whatever needs to be done here.
if(success)
success(data, textStatus, jqXHR);
};
return $.ajax(options);
}
You can do whatever you need to the usual success callback parameters before calling the original success callback. You'd call wrapped_ajax in exactly the same way as $.ajax. You could use the same technique to hook into the other callbacks as well.
try jQuery.ajaxSetup it may help you ,read about it here
Do like this:
$.ajaxSuccess(function(){
//somethingtodo
});
Mentioned in http://tutorialzine.com/2011/06/15-powerful-jquery-tips-and-tricks-for-developers/ heading twelve.

JQuery error property for error handler

I'm just playing around for the first time with jQuery's ajax functionality. I wanted to add a function that could handle any errors. So, in one of my client javascript blocks, I added the following line:
<script type="text/javascript">
....
$.ajax({ error: function () { alert('boo'); } })
....
</script>
I expected that this would bind the error handler, so that when an error occurs, it would fire the anonymous function included.
What happens instead though, is that it immediately fires the function on page load, as soon as it parses this line of code.
What am I doing wrong? What is the proper way to bind the ajax error handler?
I'm not sure if I understood your question correctly, let me know if I've misunderstood.
I assume that you are trying to create a generic ajax call error handler? If that's the case, you have got the wrong idea.
Are you are just trying to bind the event handler? In this case, you are executing it.
I would recommend you read and check out the examples on these jQuery API reference docs:
API/1.3/Events
Ajax/jQuery.ajax
Also check out the post link provided by F.Aquino and this SO post: JavaScript Exception Handling.
This is could be helpful too: Handling AJAX Errors With jQuery.
You want to change the global settings. Check jQuery documentation.
$.ajaxSetup({
error: function () { alert('boo'); }
});

Categories