0

I need to be able to make 2 ajax requests at the same time on one sharepoint page. One request comes from the current site, the other comes from a team site. If I use the different urls (/events) and (/travel) both will work on the page when used alone. There are only issues when I try to combine them (ie do the AJAX call for travel, in the succeed portion of the /events call). It works with both in edit mode, but if I save the page, it only shows items from /events, instead of also showing /travel.

$.ajax({
    url: url, //THE ENDPOINT
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(data) {

     $.ajax({
    url: urlB, //THE ENDPOINT
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(dataB) {

        for (var iB = 0; iB < dataB.d.results.length; iB++) {

            var dataStartB = dataB.d.results[iB].StartTime;
            var dataEndB = dataB.d.results[iB].EndTime;

            var isoStartB = new Date(parseInt(dataStartB.substring(6).slice(0,-2))).toISOString().replace("T"," ").split('.')[0];
            var isoEndB = new Date(parseInt(dataEndB.substring(6).slice(0,-2))).toISOString().replace("T"," ").split('.')[0];

            events.push({
                id: dataB.d.results[iB].Id.toString(),
                title: dataB.d.results[iB].Title,
                start: isoStartB,
                end: isoEndB,
                url: ("../team/_vti_bin/listdata.svc/Grouptravel" + "(" + dataB.d.results[iB].Id + ")"),
                allDay: dataB.d.results[iB].AllDayEvent.toString()
            });

        }

}
});


        for (var i = 0; i < data.d.results.length; i++) {

            var dataStart = data.d.results[i].StartTime;
            var dataEnd = data.d.results[i].EndTime;

            var isoStart = new Date(parseInt(dataStart.substring(6).slice(0,-2))).toISOString().replace("T"," ").split('.')[0];
            var isoEnd = new Date(parseInt(dataEnd.substring(6).slice(0,-2))).toISOString().replace("T"," ").split('.')[0];

            events.push({
                id: data.d.results[i].Id.toString(),
                title: data.d.results[i].Title,
                start: isoStart,
                end: isoEnd,
                url: ("../_vti_bin/listdata.svc/Events/" + "(" + data.d.results[i].Id + ")"),
                allDay: data.d.results[i].AllDayEvent.toString()
            });
        }
  • you don't seem to be passing any results from the first call to the second call, is there a reason you have embeded one inside the other? why not do 2 completely separate calls? Alternatively, try this and see if it works, if it doesn't it will be an issue with your embeded call. Finally, how are you confirming one works and one fails? is this because of the output you are getting? or do you debug and find the success code for one and the failure for the other? what is the failure code on the 2nd? – Nikerym Sep 19 '16 at 03:13

1 Answers1

0

I would suggest to chain multiple ajax calls in a promise. Check the second reply on this question and modify your js like this: How do I chain three asynchronous calls using jQuery promises?

Community
  • 1
  • 1
Verthosa
  • 1,671
  • 1
  • 15
  • 37