简体   繁体   中英

How can i pass function as parameter with arguments

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM