Passing a variable through an ajax request upon successful completion

Is there a way I can include the variable 'schedule_id[i]' in the result of this call? Can I also add this variable to the data object?

Here's my code:

for (var i = 0; i < schedule_id.length; i++) {

    //Making an AJAX request
    $.ajax({
        url: "http://api.viewer.zmags.com/schedules/" + schedule_id[i] + "?key=" + api_key
    })

    //
    .done(function(data){

}

Answer №1

Are you looking to handle asynchronous ajax calls in a synchronous manner?

One approach would be to create a separate function that makes the ajax request and returns the result, which can then be used in subsequent requests.

For example:

for (var i = 0; i < schedule_id.length; i++) {
    var result;
    if (i == 0)
        result = handleAjaxCall(0, schedule_id[i]);
    else
        result = handleAjaxCall(result, schedule_id[i]);
}

function handleAjaxCall(passedResult, schedule_id) {
    $.ajax({
        url: "http://api.viewer.zmags.com/schedules/" + schedule_id + "?key=" + api_key
    })
  .done(function (data) {
      return data;
  });
}

Answer №2

To create the ajax request, you can follow this structure:

$.ajax({
    url: 'http://api.viewer.zmags.com/schedules/',
    type: 'POST', // or GET,
    data: {
        schedule_ids: schedule_id, //list
        key: api_key
    },
    success: function (data) {
        //callback for successful response
    }
});

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

What is the best way to assess a scope object within an ng-Class directive?

Whenever I click a button, it moves to the next item in an array. Once it reaches the last item in the array, I want to assign the "endButton" class to the button tag. I plan to use the ng-class directive within the HTML code to evaluate the expression. T ...

Dependencies for Grunt tasks

I am facing some issues with a grunt task named taskA that was installed via npm. The task has a dependency on grunt-contrib-stylus, which is specified in the package.json file of taskA and installed successfully. However, when I run grunt default from the ...

Is it necessary for material-ui useStyles to use the entire props object?

Perusing through the documentation, it suggests that in order for our component to accommodate style overrides using classes, we are advised to provide the entire props object to the useStyles hook: function Nested(props) { const classes = useStyles(prop ...

When attempting to populate table cells using jQuery and Ajax, they are showing as 'undefined'

Looking for help with JQuery and Ajax integration? I have a dynamic select option in my view blade that needs to populate an HTML table based on the selected value. However, when passing the selected value to the controller in Laravel using JQuery and Ajax ...

Is there a method for redirecting my page to a specific href link without triggering a page reload?

This is my HTML code. <a href="http://127.1.1.0:8001/gembead/emstones.html?car=36">Car</a> I am trying to redirect to a specific page with parameters without fully reloading the current page. Is there a way to achieve this task? I believe the ...

Inquiries for Web-Based Applications

As I contemplate coding my very first webapp, I must admit that my skills are somewhere between beginner and intermediate in html, css, js, jquery, node, sql, and mongodb. However, the challenge lies in not knowing how to bring my vision to life. My ulti ...

Using a JavaScript "for each" loop instead of having several "if

Could you please provide guidance on where to proceed? There are multiple input rows, with each row containing a field with the class row_changed If the value in the field is greater than 0, ajax will send the entire row to PHP. Each row is wrapped in a ...

Tips for transmitting and utilizing information in ejs or jade with a node js server

I'm currently working on a project where I need to send data stored in localstorage from an ajax call to a node js server. The goal is to use the retrieved data to customize an html page using ejs or jade templates. I've attempted to send my data ...

Having trouble retrieving data from the server for the POST request

I am fairly new to using Jquery and Ajax requests. I'm currently working on a website where I have a simple form that collects an email address from users and sends it to the server. However, I'm struggling to figure out how to capture the form d ...

Parsing HTML to access inner content

Currently, I have integrated an onClick event to an anchor tag. When the user interacts with it, my objective is to retrieve the inner HTML without relying on the id attribute. Below is the code snippet that illustrates my approach. Any assistance in acc ...

Solving Promises with Arrays in JavaScript

Currently, I am working on a project and facing an issue that I need help with. Let me give you some background on what I am trying to achieve. First, I am making concurrent API calls using Axios in the following manner: const [...result] = await Promise. ...

What is the process for appending a value to an array of JSON objects?

I have a JSON array containing objects which I need to pass the values to the DataTables. [{ _id: '58a2b5941a9dfe3537aad540', Country: 'India', State: 'Andhra Pradesh', District: 'Guntur', Division: ...

Strange Interaction with Nested Divs in CSS Layouts

I am working on a webpage layout and currently it looks like this: https://i.sstatic.net/s4TOb.jpg I am trying to align the purple box inline with the pink box inside the yellow box, which is inside the green box. However, when I change the display proper ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

What is causing the undefined value to appear?

I'm puzzled as to why the term "element" is coming up as undefined. Even after running debug, I couldn't pinpoint the cause of this issue. Does anyone have any insights on what might be going wrong here? Below is the snippet of my code: const ...

The browser has surpassed the maximum call stack size while trying to refresh with socket.io, causing an error

I've encountered an issue with my Node js server crashing whenever I refresh the browser. The websocket connection works fine initially, but upon refreshing, the server crashes with the following error: E:\Back\node_modules\socket.io-pa ...

The MaterialTable is unable to display any data

After calling the fetch function in the useEffect, my getUsers function does not populate the data variable. I am unable to see rows of data in the MaterialTable as the data structure is in columns. I need help figuring out what I'm doing wrong. func ...

Are you sure you want to proceed with the deletion?

Currently, I have a Mootools code that deletes a record upon clicking a button. Now, I would like to enhance this functionality by adding a confirmation dialog box that asks the user if they are sure they want to delete the record, with options for 'Y ...

In Vue, props are not automatically assigned; be sure to avoid directly mutating a prop when assigning it manually to prevent errors

I am working with two Vue components: GetAnimal.vue and DisplayAnimal.vue. GetAnimal.vue sends a JSON object containing animal data to DisplayAnimal.vue using router push. DisplayAnimal.vue then displays this data. The process flow is as follows: I navigat ...

Adding event listeners to elements created dynamically

I am facing an issue with some dynamically generated divs from a JavaScript plugin. The divs have a class .someclass which wraps existing divs. My goal is to add a class to the children of .someclass. I attempted to achieve this by using the following code ...