Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AjaxSetup never execute the success function

Tags:

jquery

ajax

I have a simple registration page that validates if a user is already taken.

I use ajaxSetup for all my ajax calls and for some reason the "success" is never called. When I look at my console (firebug) I can see a successfully request (code 200 OK and I get true or false as the result).

Here's my code:

$('#checkValidUsername').click(function() {
    // some basic validation like not empty etc...
    $.ajax({
        type: "POST",
        url: '/checkuser.php',
        cache: false,
        data: $("#form").serialize(),
        dataType: 'json',
        success: function(result) {
            // do some actions
        },
    });
}

$.ajaxSetup({
    beforeSend: function() {
        // show loading dialog // works
    },
    complete: function(xhr, stat) {
        // hide dialog // works
    }
    success: function(result,status,xhr) {
        // not showing the alert
        alert('success');
    }
});

What is wrong with my code? Thank you

like image 355
Tech4Wilco Avatar asked Sep 12 '11 15:09

Tech4Wilco


People also ask

Why is Ajax success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

What is ajaxSetup?

The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests. Syntax: $.ajaxSetup( {name:value, name:value, ... } ) Parameters: type: It is used to specify the type of request.

What is success function Ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


6 Answers

$('#checkValidUsername').click(function() {
    // some basic validation like not empty etc...
    $.ajax({
        type: "POST",
        url: '/checkuser.php',
        cache: false,
        data: $("#form").serialize(),
        dataType: 'json',
        success: function(result) {
            myAjaxSetup.success.apply(this, arguments);
            // do some actions
        },
    });
}

var myAjaxSetup = {
    beforeSend: function() {
        // show loading dialog // works
    },
    complete: function(xhr, stat) {
        // hide dialog // works
    }
    success: function(result,status,xhr) {
        // not showing the alert
        alert('success');
    }
};
$.ajaxSetup(myAjaxSetup);

each object function that you overwrite simply

myAjaxSetup.success.apply(this, arguments);

or

myAjaxSetup.error.apply(this, arguments);

or

myAjaxSetup.anyfunctionyouwant.apply(this, arguments);
like image 130
Gary Avatar answered Oct 01 '22 16:10

Gary


because you are already using success in your $.ajax call

$('#checkValidUsername').click(function() {
    // some basic validation like not empty etc...
    $.ajax({
        type: "POST",
        url: '/checkuser.php',
        cache: false,
        data: $("#form").serialize(),
        dataType: 'json'
/*
        success: function(result) {
            // do some actions
        },
*/
    });
}

$.ajaxSetup({
    beforeSend: function() {
        // show loading dialog // works
    },
    complete: function(xhr, stat) {
        // hide dialog // works
    }
    success: function(result,status,xhr) {
        // not showing the alert
        alert('success');
    }
});

everything in $.ajax will override the $.ajaxSetup.

like image 39
balexandre Avatar answered Oct 01 '22 17:10

balexandre


Remove the success handler inside $.ajax().

like image 30
Sahil Muthoo Avatar answered Oct 01 '22 17:10

Sahil Muthoo


I think you are overriding it when you specify the success function in your ajax call. Try removing that and see if it calls the one from ajaxSetup.

like image 40
Richard Dalton Avatar answered Oct 01 '22 15:10

Richard Dalton


$.ajaxSetup() is a means of providing pre-made, default settings that apply to all your future ajax() calls unless you override them in that specific ajax() call. When you define a success handler in both the ajaxSetup() call and also in the ajax() call itself, only one of those success handlers will get called.

So, if you want the success handler to get called from ajaxSetup(), then don't define one in the ajax() call. If you define one in the ajax() call, then the one from ajaxSetup() won't get called.

like image 37
jfriend00 Avatar answered Oct 01 '22 15:10

jfriend00


You may use Global Ajax Event Handlers

$(document).ajaxSuccess(function() {
  $( ".log" ).text( "Triggered ajaxSuccess handler." );
});
like image 43
Oleg Avatar answered Oct 01 '22 15:10

Oleg