Struggling to display outcomes following an AJAX request

Browser: Internet Explorer 11

Platform: SharePoint 2016

I'm currently attempting to cache data into an array upon page load, with the intention of using these arrays efficiently throughout my code. I have four arrays that need to be populated with data from four different SharePoint lists. Using jQuery, I make calls to these lists to retrieve the necessary information. However, it seems that my current approach is flawed as the arrays are not being populated in time for me to use them. Here's a snippet of the code in question:

var cacheNavData = [];
var cacheSubNavData = [];
var cacheMegaMenuData = [];
var cacheCategoryMenuData = [];

$(document).ready(function(){
  getNavData();
  getSubNavData();
  getMegaMenuData();
  getCategoryMenuData();
})

function getNavData(){
// function body
}

function getSubNavData(){
// function body
}

function getMegaMenuData(){
// function body
}

function getCategoryMenuData(){
// function body
}

console.log(cacheNavData);
console.log(cacheSubNavData);
console.log(cacheMegaMenuData);
console.log(cacheCategoryMenuData);

It appears that my issue lies in the asynchronous nature of the calls, and I am still trying to grasp this concept fully. Despite reading various explanations and watching videos on the subject, I seem to struggle with finding a solution to this problem. When checking the console.log(), it's evident that the Ajax calls haven't returned the data yet. While I understand the root cause, I'm unsure how to rectify or prevent this delay. I attempted the following solution, but it proved unsuccessful. Any assistance would be greatly appreciated. Thank you!

// Updated code attempt
var cacheNavData = [];
var cacheSubNavData = [];
var cacheMegaMenuData = [];
var cacheCategoryMenuData = [];

$(document).ready(function() {
    var cache1 = getData("Navigation", "cacheNavDataVar");
    var cache2 = getData("Sub Navigation", "cacheSubNavDataVar");
    var cache3 = getData("category menu", "cacheCategoryMenuDataVar");
    var cache4 = getData("Mega Menu Category", "cacheMegaMenuDataVar");

    $.when(cache1, cache2, cache3, cache4).done(function(results){
        if(results){
            createNavigation(cacheNavData)
        }
    })

});

Answer №1

https://i.sstatic.net/vzjcz.png It should be clear from the highlighted text above. When multiple asynchronous Ajax requests are made, the browser will open a new TCP port for each request. Once a response is received for any of these requests, the Ajax success function will begin execution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to dynamically create form controls within Angular forms and receiving the following error in the console: "TypeError: feature_r5.get is not a function"

When I click a button in my Angular v14 HTML template, I am trying to dynamically generate form controls and although I am able to generate them, an error is popping up in the console. ERROR TypeError: feature_r5.get is not a function at PostAdvComponent_ ...

Struggling to grasp the concept of async.parallel in Node.js

Within the controller, I have implemented the following function: router.post('/', function(req, res, next) { if (req.user.isPremium == false) { // As a free user, only a single report is generated let website = req.body.website0; ...

Generate a novel item by organizing the key of an array of objects

I have an array of objects containing various items: [ { CATEGORY:"Fruits" ITEM_AVAIL:true ITEM_NAME:"Apple" ITEM_PRICE:100 ITEM_UNIT:"per_kg" ITEM_UPDATED:Object ORDER_COUNT:0 }, { CATEG ...

Tips for verifying/employing an md5 hash on a request stream in Node.js?

When storing data in the MongoDB database using GridFS, I directly stream the request to the write stream where it gets stored with an md5 hash. Now, my next task is to store duplicates in the database by comparing the md5 of new data with existing entri ...

Redirecting users based on their type - Vue router

Seeking assistance as a novice in vue. Building an app with firebase-connected login. Want to utilize vue-router for user redirections. Goal: Redirect "admin" users to "/admin", others to "/", and non-logged-in users to "/login". Sharing parts of my code: ...

I am looking to efficiently store various pieces of data in a database by utilizing a singular variable through JS, PHP, and AJAX for streamlined processing and management

I am not very familiar with the technical jargon in programming, so please bear with me if my question is a bit unclear. To provide more clarity, I have shared the code that I have already written. I will elaborate on the issue after presenting the code: ...

I am currently working with an input element that is set to hidden. I am trying to change its type to text using JavaScript, but I can't seem to figure out how to do it or if it is even possible

I'm stuck trying to change the type of an input element from hidden to text using JavaScript. I can't seem to figure out if it's even possible. Can someone please help me with this? Thanks! ...

Whenever I enter a narrative into my text box, it must interact with this API

Whenever I enter a story in the text box, it should trigger this API call: The retrieved data should be displayed in the browser. Currently, the API call is not being made. All the relevant code can be found in 'searchbar.js'. Could you please ...

How can I prevent media controls from appearing in fullscreen mode on Microsoft Edge using CSS?

My video code allows for switching the video into fullscreen mode using custom controls. Here's how it looks: fullScreenButton.addEventListener("click", function() { if (video.requestFullscreen) { video.videoContainer.r ...

Using bootstrap can alter the positioning of elements that have margins relative to other elements

After exploring suggestions from my previous post on Stack Overflow, I have implemented a modified version to animate drag movements. While the HTML5 draggable attribute can achieve dragging, its limitations and side effects are something I aim to avoid. ...

Exploring the capabilities of Firebase functions within a Vue.js environment

I've been working on unit testing my Vue components, but it's posing challenges due to the integration with Firebase. To address this issue, I decided to set up a dedicated folder named __mocks__ for storing all my mocked functions. Within this f ...

What is the best method to iterate through a jQuery array variable without any errors?

I am struggling with a PHP project where I need to create a json array of data values that will be used in a jQuery script. The array looks something like this: [3,94,83,141]. My goal is to leverage these values to manipulate the visibility of rows in a ta ...

Hiding a Div Using jQuery Depending on User's Choice

Currently, I am in the process of developing an employee directory using AJAX/jQuery with the assistance of the Random User Employee Directory API. You can access the data feed that I am utilizing by following this link: I have successfully created a webp ...

Error will be thrown if the initialDueDate parameter is deemed invalid in Javascript

Can someone help me improve the calculateNextDueDate function which takes an initialDueDate and an interval to return the next due date? I want to add argument validation to this function. Any suggestions would be greatly appreciated. Thank you! const I ...

The series attribute cannot be located within the reactD3Basic object in ReactD3

Recently, I attempted to integrate the reactd3 library into my project and encountered an issue: Uncaught TypeError: (0 , _reactD3Basic.series) is not a function This error is specifically pointing to this line in the library code: var chartSeriesData = ...

Searching for data in a bootstrap table that is not case-sensitive

Searching for and highlighting text within a bootstrap table has proven to be a challenge. Check out the Fiddle for an example: https://jsfiddle.net/99x50s2s/74/ HTML <table class='table table-bordered'> <thead> <tr& ...

Transferring text from the main section into structure c

I'm facing an issue where I have a number of strings stored in a 2-dimensional array called "info" and I need to copy them into another array named "reply" inside the "notes" structure. Despite trying to achieve this using the following for loop, the ...

In PHP and MySQL, increment the value by 1, then subtract 1 from the subsequent value for all records in the database using their

I have been racking my brain for hours trying to solve this PHP logic puzzle, but unfortunately, I haven't had any success. Even after Googling it, I couldn't find a clear answer. Here's the problem: In my database, I have serial ID values ...

My Node setup is not displaying my scene with the THREE.js Software Renderer

Struggling to incorporate 3d graphics into Node.js environment, I stumbled upon the Software Renderer after exhaustive research. The renderer is up and running, but I am facing difficulties in rendering my scene. The issue lies in the fact that my 3d obje ...

Implementing CORS in ReactJs: A Step-by-Step Guide

When working with React.js, I often use this fetchData config: fetchData = () => { fetch("http://localhost:8000/batch_predict", { method: "POST", headers: { 'Accept': 'application/json, text/plain, */*&apo ...