1

Background: I have to show product options existing in database in a form. every product has multiple options and every option has multiple values. I have 2 functions, one to fetch product options, and the other to fetch values related to an option. I am calling the second function inside the first.

Problem: while OptionValues function in getting a correct JSON Response, i am not able to pass response values correcty to the getProductOptions function in order to show them.

Option values function:

  function optionV(option_id){
 var dataString  = 'option_id=' + option_id ;
 $.ajax({
 type: "GET",
 url:  'index.php?route=new/orders/OptionValues',
 data: dataString,
 cache: false,
 dataType: 'json',
  success: function(values) {
  return values;
   }
});

}

getProductOptions function :

function getProductOptions(sku){
  // remove previous content in #options div
document.getElementById('options').innerHTML = "";
var dataString  = 'sku=' + sku ;
// Get product options 
$.ajax({
type: "GET",
url:  'index.php?route=new/orders/productOptions',
data: dataString,
cache: false,
dataType: 'json',
 success: function(data) {
  var options = '';
 //loop through options to get values and show
 for (var i=0 ;i< data.length;i++) {
//call function optionV to get option values
var optionVal = optionV(data[i].ID);
 alert (typeof(optionVal));
  }
 });
}

the alert says : Undefined. and any attempt to access the optionV result in the other function is failing.

I have already tried stringify and json.parse

What should I do?

Siba Al
  • 357
  • 1
  • 14
  • Async function... – Weedoze Mar 20 '17 at 09:30
  • You can use Promise to get data from your async function. Try the following: `function optionV(){return new Promise((resolve,reject) => { $.ajax{...,success: (data) => {resolve(data)}}})}` and in **getProductInfo** do: `optionV().then((data)=>{//do somethin with data});` – Apoorv Joshi Mar 20 '17 at 09:52
  • @ApoorvJoshi Thank you – Siba Al Mar 20 '17 at 13:19

0 Answers0