Can a jQuery $.post call itself again from the callback?

jquery ajax post json
jquery ajax post example
jquery ajax post example php
jquery post headers
jquery post content-type
asynchronous ajax call jquery example
jquery ajax error
jquery post form data

The use case would be if the response it gets isn't what it wanted it can call itself again.

$.post(qrLoginAjax.ajaxurl, { 
    userID : '11234324'
},function( response ) {
    if (response.userID != 'undefined'){
        //do stuff
    } else {
        // call $.post again
    }
});

How would I do that?

Thanks

You could do something like this:

var sendAjax = function() {
    $.post('/foo', function(result) {
        if (response.userID != 'undefined') {
            // do stuff
        } else {
            // resend the AJAX request by calling the sendAjax function again
            sendAjax();
        }
    });
};

sendAjax();

But sending AJAX requests like this seems like a bad design decision in my opinion. You should ensure that you don't get into an infinite recursion by using for example a number of retries counter.

jQuery.post(), You could do something like this: var sendAjax = function() { $.post('/foo', function​(result) { if (response.userID != 'undefined') { // do stuff } else { // resend the  jQuery Callback Functions. JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. To prevent this, you can create a callback function. A callback function is executed after the current effect is finished.

You can make it a function, and call itself. For example with $.ajax, you can do this:

function do_stuff(){
    $.ajax({
        url: 'ajax.php',
        success: function(data){
            // Stuff
        },
        error: function(){
            do_stuff();
        }
    });
}

This is the general principle of a recursive function, but it is highly recommended that you test conditions or set a maximum number of tries so it doesn't get stuck in an infinite loop.

jQuery.ajax(), A callback function that is executed if the request succeeds. Required if dataType is provided, but can be null in that case. dataType. Type: String. The type of  The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response. As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

Use this keyword of Javascript. As we all know, that refers to the object it belongs to. For example:

$.post(qrLoginAjax.ajaxurl, 
      {
        userID : '11234324'
      }, 
      function( response ) {
          if (response.userID != 'undefined') {
             //do stuff
          } 
          else {
            $.post(this);
          }
      }
);

jQuery.Callbacks(), See jQuery.ajax( settings ) below for a complete list of all settings. A pre-​request callback function that can be used to modify the jqXHR (in jQuery the settings; if context is not specified, this is a reference to the Ajax settings themselves. json ) to a custom data type and back again, use another pass-​through converter:  Hi there first a great hello to all the people discussing here, I am the new one :-) New at both this list and jQuery itself. So here is my first question, [jQuery] Callback with .post() fails too often - jQuery Forum

AJAX & Deferreds, ajax() and $.Deferred() components. It can be used as a similar base to define functionality for new components. $.Callbacks() supports a number  The above code shows you how to create a callback function for AJAX load, but if you want to create a simple callback function for your jQuery plugin, for example the one that executes on your own custom events. To achive this you can do the following:

Beginning JavaScript and CSS Development with jQuery, We can use the jQuery $.ajax() method in a couple of different ways: we can pass it a Create the "callback" functions that will be invoked when. understand the difference between the JSON representation of an object, and the object itself. You might have seen some controller logic where a function inside a controller calls another one using this.anotherFunction. You can definitely do that if the context on which the first function is called is the controller itself. As it happens with the “hook” functions onInit and onExit for example. So where is the catch?

How to queue (more than one) Ajax calls using jQuery without , Since the $.ajax() method works by calling that method of the jQuery object directly, called self so that you can reference the variable self from within the callback method itself takes various options defined as an object literal, which again  A callback function is a function that is executed once the effect is complete. The callback function is passed as an argument to the effect methods and they typically appear as the last argument of the method. For example, the basic syntax of the jQuery slideToggle() effect method with a callback function can be given with: