0

I would like to know how to pass value in array to the ajax function in javascript. For each id, call the function in javascript. How to pass each id in array and call the function

var listid=["fund","trans","ins"]; 

getData(id){
var li = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  }).responseText;
 console.log(JSON.parse(li));

}
Senthil
  • 961
  • 1
  • 8
  • 21

6 Answers6

1

You can use:

listid.forEach(function (id) { getData(id) });
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Sai Kiran
  • 26
  • 3
0

You could use the map function for arrays. It is a JavaScript function that takes a callback as a parameter. You can pass any function as a callback and it will call it for every value in that array.

SerShubham
  • 873
  • 4
  • 10
0

Apply loop to your id array.

var listid=["fund","trans","ins"]; 

for(let i = 0, len = listid.length; i < len; i++) {
    getData(listid[i]);
}
random
  • 7,756
  • 3
  • 19
  • 25
0

you can use $.each for your array value

var listid=["fund","trans","ins"]; 
    $.each(listid, function( index, value ) {
      //console.log( index + ": " + value );
      getData(value );  //uncomment this for actual implementation
    });
    
    function getData(id){
    var li = id;
    /*$.ajax({
      url: "/en",
      method: 'get',
      global: false,
      async: false,
      data: {
        idvalue: id
      },
      success: function(value) {
        return value;
      }
    }).responseText;*/
    //console.log(JSON.parse(li));
    console.log(li);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0

var listid=["fund","trans","ins"]; 

for(var i = 0 ; i < listid.length ; i++){
getData(listid[i]);

}
function getData(id){
var li = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  }).responseText;
 console.log(JSON.parse(li));

}
Sandeep K.
  • 759
  • 6
  • 18
0

You can iterate over the list using a forEach easily.

var listid=["fund","trans","ins"]; 

listid.forEach(function(id) {
   getData(id);
});

function getData(id) {
   var xhr = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  }).always(function(data,  textStatus, jqXHR) {
      // deal with the data
  })
}

If you are using newer version of Jquery then The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

function getData(id) {
   var xhr = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  })
  .done(function(data, textStatus, jqXHR ) {
    // do something with value
  })
  .fail(function(jqXHR, textStatus, errorThrown) {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
}
Saroj
  • 1,551
  • 2
  • 14
  • 30