Forum Discussion

Shivshankar's avatar
8 years ago

How many ajax calls we can make on a custom page?

Hi,

 

I have created a custom page , how many ajax calls we can make on the page ? Is there any restriction on number of api calls and is there any setting available to configure this ?

8 Replies

  • Shivshankar

    There are no such restrictions, you can make any number of ajax call.

    On that custom page, you are using a custom component having JS so in JS you can make any no of ajax call it does not depend on that custom page. LMK what you are trying to do so can help you out more. 

  • i have a code where in i am processing bulk ideas , atleast 1000 minimum with updating some comments and changing status of them.

    On a certain instance i am getting Error 429
    https://httpstatuses.com/429 

    so i am unable to understand what is the cause of this error.

    Is it because of too many calls ? . On some other instance i am not facing such issues.

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Shivshankar

    I would also suggest you try it with few calls instead of too many calls. It would be great if you can share the code snippets. 

    As you mentioned that on some other instance, i am not facing such issues. It means the issue is not with multiple calls. You should reach out to support for instance-specific issues.

  • request_str is string with 1000 ideas comma seperated.

     

    var idArray=request_str.split(',');

    var url1=endpointi?request_str="someId"&statusid="StatusId"&post_comment="postComment";
    var saveData = $.ajax({
    type: 'POST',
    dataType: "json",
    async:"false",
    url: url1,
    error: function (jqXHR, exception) {
    //some code
    },
    success: function(resultData) {

    //somecode
    }
    });

     

    This code is giving error 429 

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Shivshankar 

    It works fine on other instances means might be (if you are creating the id's array manually) in this certain instance, some of the ideas do not exist with the given id in your array.  So can you give it a shot with few ids(like 5 ids)which exist in this instance? So both the things(too many calls and valid ids) can be tested in a single shot.  Share the endpoint call you are using if it does not work for you. 

  • luk's avatar
    luk
    Boss
    8 years ago

    This code can almost certainly not work, at least not in the way you provided the example, so giving you advice is hard here...

     

    1. what are your "ideas"? are they integer ids, or JSON objects or something else?

    2. if so, do you process each idea separately, if so, where is the $.each() loop?

    3. You send a POST request and use a URL with GET parameters...just sayin'...

    4. And of course you're getting a https://httpstatuses.com/429 error like this, no properly configured server should allow something like this, otherwise every 10 year old could DoS that server...

     

    That means you have to properly set up a request queue that doesn't overwhelm the server with 1000 requests in less than a second... (use console.log() to see how quickly your code wants to send requests...), you can do that in various ways, I'm usually doing such things with a modified version of the ajaxQueue jQuery Plugin form here, it uses promises/deferred objects and I custom-added a delay option  as I'm also using it to throttle requests sometimes, the modified plugin code is included below and after plugin code you find an example that you can run yourself (open the inspector > console to see what it does, but basically it delays each request for 1000ms = 1s):

     

    (function($) {
    
    // jQuery on an empty object, we are going to use this as our Queue
    var ajaxQueues = [$({})];
    
    $.ajaxQueue = function( ajaxOpts, queueIndex ) {
        var jqXHR,
            dfd = $.Deferred(),
            promise = dfd.promise(),
            ajaxQueue;
    
        // allow multiple queues that run independently of each other
        // specify the queue to be used like this (index can be ommited!):
        // $.ajaxQueue({<ajaxOpts>}, <int|queueIndex>)
        queueIndex = queueIndex || 0; // get queue index if specified
        ajaxQueue = ajaxQueues[queueIndex];
    
        if ( ajaxQueue == undefined || !(ajaxQueue instanceof $) ) {
            ajaxQueue = ajaxQueues[queueIndex] = $({});
        }
    
        //  if there is no ajax request return an empty 200 code
        if ( typeof ajaxOpts == "undefined" ) {
            return $.Deferred(function() {
                this.resolve( [ '', '200', jqXHR ] );
            }).promise();
        }
    
        // run the actual query
        function doRequest( next ) {
            setTimeout(function() {
                jqXHR = $.ajax( ajaxOpts );
                jqXHR.done( dfd.resolve )
                    .fail( dfd.reject )
                    .then( next );
            }, ajaxOpts.delay||0);
        }
    
        // queue our ajax request
        ajaxQueue.queue( doRequest );
    
        // add the abort method
        promise.abort = function( statusText ) {
    
            // proxy abort to the jqXHR if it is active
            if ( jqXHR ) {
                return jqXHR.abort( statusText );
            }
    
            // if there wasn't already a jqXHR we need to remove from queue
            var queue = ajaxQueue.queue(),
                index = $.inArray( doRequest, queue );
    
            if ( index > -1 ) {
                queue.splice( index, 1 );
            }
    
            // and then reject the deferred
            dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] );
            return promise;
        };
    
        return promise;
    };
    
    })(jQuery);
    
    
    $(function() {
    	// just for demo purposes, this would be your $.each()...
    	for (var i=0; i<10;i++) {
    		$.ajaxQueue({
    			url: 'https://httpbin.org/get',
    			delay: 1000
    		}).done(function(data) {
    			console.log('success');
    			console.log(data);
    		}).fail(function() {
    			console.log('failed');
    		});
    	}
    });  

     

  • luk  Nice points. 

    You can use async: false instead of delay parameter.  Next call to the endpoint would not be trigger until first call's response comes at the component.  It would be more efficient and reliable. 

    	for (var i=0; i<10;i++) {
    		$.ajaxQueue({
    			url: 'https://httpbin.org/get',
    			async: false,
    data: {} }).done(function(data) { console.log('success'); console.log(data); }).fail(function() { console.log('failed'); }); }

     

  • luk's avatar
    luk
    Boss
    8 years ago

    VikasB yeah, but async (afaik) is mainly about "outsourcing" the AJAX calls to background tasks, e.g .make them non-UI-blocking, has nothing to do with throttling the requests, e.g. you will still overwhelm the server and trigger a 429 Error when async is set to false, delay is about dealing with rate limits, e.g. for OPs problem that is what is needed to not trigger the Firewall... DougS for that purpose it would be interesting what the rate limit is for endpoints?