1

I have many $.post, $.ajax call, they all returns with a JSON array. I realized that sometimes I have to check status codes, e.g. check if user is dropped. Thats ok, but now I have to put this status-check for all occurrences. By accindent, cant I set a default callback somehow?

5
  • 1
    what about the beforeSend option? api.jquery.com/jquery.ajax Commented Mar 21, 2016 at 13:00
  • 1
    why the down vote, its a legitimiate question. @john smith, your best bet would be to wrap your post or ajax call inside a function, and handle everything there Commented Mar 21, 2016 at 13:01
  • 1
    You can wrap those function with yours, something like this: stackoverflow.com/questions/15531722/… Commented Mar 21, 2016 at 13:01
  • 1
    api.jquery.com/jquery.ajaxsetup - This allows you to set up default callbacks. Commented Mar 21, 2016 at 13:05
  • 1
    You want to check status after ajax response right? Commented Mar 21, 2016 at 13:27

3 Answers 3

1

You can use beforesend method of AJAX

beforeSend: function (xhr) {
    // It will fired each just before each AJAX request
    }
Sign up to request clarification or add additional context in comments.

Comments

1
$.ajax({
        url: '',
        beforeSend: function ()
        {
                 // Here your code it fired just before ajax request
        },
        data: {

        },
        success: function (data) {
        }
        })

Comments

1

You can use:-

.ajaxSend()

Description: Attach a function to be executed before an Ajax request is sent. This is an Ajax Event. Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend event. Any and all handlers that have been registered with the .ajaxSend() method are executed at this time.

.ajaxSend( handler )

Type: Function( Event event, jqXHR jqXHR, PlainObject ajaxOptions ) The function to be invoked.

Usage:-

$(document).ajaxSend(function( event, jqxhr, settings ) {
   // This will be fired before all ajax requests
});

NOTE

If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxStart() method will not fire.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.