使AJAX变量可用于运行

时间:2017-09-29 08:12:23

标签: javascript jquery ajax

我有一个函数被调用来在表中创建一个行,传入行数据。在该函数中,我需要调用一个AJAX函数来检索与行中某个单元格相关的数据。当我通过.done方法得到变量时,我很难看到如何在fctn1函数中使用它。

function fctn1( rowdata ) {
  var url = "https://...";
  var state;
  fetchData(url).done(function(data) {
    var state = data.items.state;
    console.log(state);    // logs correct state
  });
  console.log(state);    // undefined
    // fctn1 code creates table row using rowdata and 'state' variable from AJAX call
}

function fetchData(url) {
  // Return the $.ajax promise
  return $.ajax({
    url: url,
    type: "GET",
    contentType: "application/json",
    dataType: 'json',
  });
}

0 个答案:

没有答案