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

3
  • 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
    Commented Oct 5, 2018 at 8:27
  • It should be working fine jsfiddle.net/sfu9kt5h/7 Commented Oct 5, 2018 at 8: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
    Commented Oct 5, 2018 at 8:30

1 Answer 1

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);
1
  • did you check the fiddle?
    – eavichay
    Commented Oct 5, 2018 at 8:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.