Skip to main content

Ajax call inside for loop issue and solution


Hello people, This post is regarding real day to day issue in coding and its resolution, It sometimes looks small issue but when it starts eating your time you start getting feeling that its no more smaller one, Here goes the issue

As the title of the blog suggests, When you are making AJAX calls inside loop, It is finitely possible that inside loop when the call is made, It takes time to get the result back to client side and by that time loop has already ended and counter has reached to limit and you end up thinking how to keep track of the request, it should get the correct index in "Success" method of $.ajax call, Otherwise it will keep on giving you one count higher than your total count of loop, There can be many way as we know in our world but below is one of the good solution to it

Solutions
See below code for an example

for (var i = 0; i < groupCount.length; i++) 
{
                !function (i) {
                    $.ajax({
                        type: 'POST',
                        url: ,
                        data: data,
                        dataType: "json",
                        success: function (respons) {
                            // Do some action
                            
                        },
                        error: function (err, para1, parra2) {
                           // Do some action 
                        }
                    });
                } (i);

Here we can see we have wrapped the ajax call into !function(), It will make sure that your ajax call works, no matter what !!!, One more dirty fix is that we can change the property called async=false in $.ajax parameters but that does not serve the purpose as when we use ajax it has to be async.

Small but helpful !!!

Comments

  1. this does not work every time..results are sporadic....i had 2 ajax calls inside of for and and only one runs....and ofcourse...sometimes both.

    ReplyDelete
    Replies
    1. If there are two ajax calls you need to tweak them, so just like one call you need to change the function for two calls

      Delete

Post a Comment

Popular posts from this blog

An error occurred while receiving the HTTP response to This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

You have noticed many times that everything was working fine and suddenly the below error starts coming and you find no way to work it out An error occurred while receiving the HTTP response to This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. The reason for this is the receiving size of WCF service is smaller then the data which is coming from service It was working before because it was small,So you will have to try to increase the receiving setting in your end point,Possible settings can be following maxStringContentLength="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxArrayLength="2147483647" That would definately help you!!!

One or more exceptions occurred while processing the subscribers to the 'item:creating' event

I was recently installing the packages from one of the QA environment to my local Sitecore instance, "Media library package" to be precise, And it started giving me this below error One or more exceptions occurred while processing the subscribers to the 'item:creating' event Looking at the sitecore logs, it gave me more info on the context and the actual inner exception was following Solution: 'Name' should consist only of letters, digits, dashes or underscore Now it was evident that some of my file names were violating the naming rule, I could see in the log just before exception from where the installer stopped creating items, and that file name had round braces "(" and ")" at the end of it, with my surprise I was able to create the item with those name in the content tree, but below was the solution for it, Solution I am using SC 9.0.1 and in that Go to Sitecore.Marketing.config file residing in "App_Config\Sitecore\Mar

Sitecore Serialization - Error connecting to /.well-known/openid-configuration: Bad Gateway

Hi Team, Recently, I faced an issue where for the new project my Sitecore serialization started giving me following error. Error: "Error connecting to https://sc10.dev.local/.well-known/openid-configuration: Bad Gateway" My identity server when browsed above url was also not working and showing following. Well, I tried seeing logs and different things, I also followed what is given on these links.  https://sitecore.stackexchange.com/questions/34744/open-id-configuration-issue-with-sitecore-cli https://tekkishare.azurewebsites.net/pages/sitecore-clilogin-error-badgateway https://www.stockpick.nl/english/sitecore-cli-login-error/ But I was still getting the same error. Solution If you see above screenshot of my identity server, in "Troubleshooting steps" it is mentioned that "Check the system event log for error messages." It triggered me, I opened event log of windows, and I could see following error.  Now, i already update my .net run time version accordin