How to pass additional parameters to Ajax response when sending request


user

I have a jquery function that iterates through an array and calls a servlet for each iteration. How can I make sure that each servlet's response reaches the corresponding div section. The div ID is unique and corresponds to the val in the array.

jQuery:

function foo() {

  for (var p in obj) {

    var tagId = obj[p]["prop1"];

    $.get('CallServlet', {
      json: JSON.stringify(obj[p])
    }, function(response) {
      $("#" + tagId).append(response);
    });

  }
}

example:

tagId= obj[0][val1];

I want to respond:

<div id="tagId"></div>

In the above function, the value of tagId in the response will be overwritten as the iterator traverses the object. When I receive the response obj[0], the tagId will be updated toobj[1][val1]

Abdullahman Alam

You need to wrap it with an IIFE function

function foo() {

  for (var p in obj) {

    var tagId = obj[p]["prop1"];
    (function(id) {
      $.get('CallServlet', {
        json: JSON.stringify(obj[p])
      }, function(response) {
        printFunc(responseText, id);
      });
    })(tagId)

  }
}

Related


How to pass additional parameters to jQuery DataTable ajax call?

Brett I have the following code when jQuery DataTable is loaded. How can I pass additional parameters to the AJAX call? The fnServerParams callback suggested in the following question and answer does not work. That is, naively using the result aodata.push()in

How to pass additional parameters to jQuery DataTable ajax call?

Brett I have the following code when jQuery DataTable is loaded. How can I pass additional parameters to the AJAX call? The fnServerParams callback suggested in the following question and answer does not work. That is, naively using the result aodata.push()in

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

How to pass additional parameters to jQuery DataTable ajax call?

Brett I have the following code when jQuery DataTable is loaded. How can I pass additional parameters to the AJAX call? The fnServerParams callback suggested in the following question and answer does not work. That is, naively using the result aodata.push()in

How to pass additional parameters into the event

Doug Fir I have a series of clickable links "btns" for ( var i=0; i<btns.length; i++ ) { btns[i].addEventListener('click', eventData, false); The eventData function is here: function eventData(e) { dataLayer.push({ 'event': 'home_cta',

How to pass additional parameters along FormData in $.ajax

Jacobdo I am using FormData to upload images and I cannot pass other parameters through it. I want all ajax calls to fall on the same script (ie ajax.php) and execute part of the script based on other parameters passed. My ajax call looks like this: $(document

How to pass additional parameters along FormData in $.ajax

Jacobdo I am using FormData to upload images and I cannot pass other parameters through it. I want all ajax calls to fall on the same script (ie ajax.php) and execute part of the script based on other parameters passed. My ajax call looks like this: $(document

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

How to pass additional parameters to UITapGestureRecognizer?

fourth space How to pass floating point value to function handleTap? It seems that my parameter signature is set correctly, but there is no way to pass float value. let tapped = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:digit:)))

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

How to pass additional parameters to ajax callback function?

username I am making an ajax call like this: util.AjaxCall(url,successCallbackFunction,errorCallbackFunction); function successCallbackFunction(result){ //Ajax returns result } Everything works fine except I have to pass one of my own parameters to be use

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

How to pass additional parameters into the event

Doug Fir I have a series of clickable links "btns" for ( var i=0; i<btns.length; i++ ) { btns[i].addEventListener('click', eventData, false); The eventData function is here: function eventData(e) { dataLayer.push({ 'event': 'home_cta',

How to pass additional parameters to jQuery DataTable ajax call?

Brett I have the following code when jQuery DataTable is loaded. How can I pass additional parameters to the AJAX call? The fnServerParams callback suggested in the following question and answer does not work. That is, naively using the result aodata.push()in

How to pass additional parameters into the event

Doug Fir I have a series of clickable links "btns" for ( var i=0; i<btns.length; i++ ) { btns[i].addEventListener('click', eventData, false); The eventData function is here: function eventData(e) { dataLayer.push({ 'event': 'home_cta',

How to pass additional parameters along FormData in $.ajax

Jacobdo I am using FormData to upload images and I cannot pass other parameters through it. I want all ajax calls to fall on the same script (ie ajax.php) and execute part of the script based on other parameters passed. My ajax call looks like this: $(document

How to pass additional parameters along FormData in $.ajax

Jacobdo I am using FormData to upload images and I cannot pass other parameters through it. I want all ajax calls to fall on the same script (ie ajax.php) and execute part of the script based on other parameters passed. My ajax call looks like this: $(document

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

How to pass additional parameters to useSelector

Andy Hoffman I am useSelectorsuccessfully calling from a component which derives the product name from the id. const productId = 25; // whatever const productName = useSelector( (state) => state.dashboard.dashboards.filter( ({ Id }) => Id === prod

How to pass additional parameters to ajax callback function?

username I am making an ajax call like this: util.AjaxCall(url,successCallbackFunction,errorCallbackFunction); function successCallbackFunction(result){ //Ajax returns result } Everything works fine except I have to pass one of my own parameters to be use