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!!!

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

Set up leprechaun code generation with Sitecore XM Cloud Starterkit

Hi Sitecorians, It has been amazing learning year so far and with the change in technology and shift of the focus on frontend frameworks and composable products, it has been market demand to keep learning and exploring new things. Reasons behind this blog Today's topic is something that was in my draft from April-May, and I always thought that there is already a good documentation out there for  Leprechaun  and a blog post is not needed, Until I realized that there was so many of us facing same kind of issues and same kind of problems and spending same amount of time, That is where I thought, if I could write something which can reduce that repetitive troubleshooting time, That would really help the community. 1)  In a project environment, if we get into some configuration issues, we resolve them, we make sure we are not blocked and continue, but if you think same issue, same step and same scenario will come to other people, so if we can draft it online, it will help other people 2