2

I have the following code, where I am trying to execute a nested ajax request to display eventually a dynamically created bootstrap carousel. But I think something goes wrong, because those two requests are not syncronized. the console.log(i) displays i=0,1,2 and after that it gets inside the nested ajax request where console.log("i equals "+i) returns i equals 3. As a result I cannot update the global variables inside the inner ajax reuest. I have already tried to add async : false but it gives me a warning. Do you have any other suggestions on the matter?

<script>
    var htmlMainSliderIndicatios="";
function LoadMainSliderContents (){
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/Container?$expand=Frame_id&$filter = Frame_id/Position eq 1",
             method: "GET",
             headers: { "Accept": "application/json; odata=verbose" },
             success: function (data) {
              for(var i=0; i<data.d.results.length;i++){
                console.log(i)
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/ContentsTranslation?$select=Title0,Subtitle,Content,LanguageValue,StatusValue,Content_id/StatusValue,Content_id/Conta_idId,Content_id/PictureXS&$expand = Content_id&$filter = Content_id/Conta_idId eq "+data.d.results[i].Id+" and LanguageValue eq 'en' and StatusValue  eq 'ongoing'",                                                          
                     method: "GET",
                     headers: { "Accept": "application/json; odata=verbose" },
                     success: function (tasks) {
                        console.log("i equals "+i)                  
                        $.each(tasks.d.results, function(bindex, task) {
                        });
                     },
                     error: function (data) {
                        console.log("fail");
                    }
                });//end of inner ajax
    }//end of outter for
        },
        error: function (data) {
            console.log("fail");
        }
        });
}
_spBodyOnLoadFunctionNames.push("LoadMainSliderContents");

</script>
0

1 Answer 1

2

What globals do you want to set?

Ajax calls are like giving 3 people the assignment:

  • Get me a coffee,
  • Get me a donut
  • and Get me a paper.

You do not have control over when they return.

async:false can help, but will take longer to finish..

  • because the donut guy now has to wait for the coffee guy to return,

  • and the paper guy won't leave until the donut is delivered.

What error message do you get with async:false ?

If you need Order you need to tell those guys what Order number they have

and NOT rely on information from the outer loop

  1. Get me a coffee, you are number 1
  2. Get me a donut, you are number 2
  3. Get me a paper,, you are number 3

Then when a guy returns you can ask his number and you know what task he had.

J5 JX

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.