0

I have ajax call function which have parameter function and it's looks like this

function selectedLang(func) {

     let selected = selectLang.options[selectLang.selectedIndex].value;

     $.ajax({
          url: 'lang.php',
          type: "POST",
          dataType: 'json',
          data: { language: selected },
          success: function (data) {
             // call here displayData function with parameter
              func(data);
          }
      });
}

inside success i want to call function displayData with parameter data

  function displayData(data) {  
    // some data  
  }

 selectedLang(displayData);

Right now i'm getting an error that func is not a function

Andrew
  • 1,507
  • 1
  • 22
  • 42
  • i will use selectedLang() function to display different data from different functions, so this will not work as i expecting! I need pass function as a parameters – Andrew Oct 05 '18 at 08:27
  • It should be working fine https://jsfiddle.net/sfu9kt5h/7/ – Nenad Vracar Oct 05 '18 at 08:29
  • The problem here might be the ajax call. Try putting the parameter function in a new variable inside selectedLang: `selectedLang(func){ let callback = func; [...] success: callback` – Danmoreng Oct 05 '18 at 08:30

1 Answers1

1

It looks like it is working. https://jsfiddle.net/bpomehv5/

Check out this fiddle

function selectedLang(func) {

     $.ajax({
          url: 'https://randomuser.me/api',
          type: "GET",
          dataType: 'json',
          success: function (data) {
             // call here displayData function with parameter
              func(data);
          }
      });
}

function showIt (data) {
    console.log(data);
}

selectedLang(showIt);
eavichay
  • 507
  • 2
  • 7