简体   繁体   English

jquery返回ajax响应和用户定义的变量来起作用

[英]jquery Return ajax response and user defined variable to function

I am having an issue passing in a defined variable to a function from an ajax call. 我有一个问题是将定义的变量传递给ajax调用中的函数。 What should happen is on click of an element, it should pass the text of the clicked element to the function, do an ajax call which has nothing to do with the defined variable, return the variable and the data from the call to another function that renders the elements. 应该发生的是点击一个元素,它应该将被点击的元素的文本传递给函数,做一个与定义的变量无关的ajax调用,将变量和数据从调用返回给另一个函数呈现元素。 When I get to the function that renders the html content, the variable is now undefined. 当我到达渲染html内容的函数时,变量现在是未定义的。

My question is how can I send the ajax response data and a user defined variable that has no correlation to the ajax call to another function? 我的问题是如何将ajax响应数据和与ajax调用无关的用户定义变量发送到另一个函数?

My JSON function: 我的JSON功能:

function getJSONP(url, data) {
  return $.ajax(url, {
    dataType: 'json',
    data: data
  });
}

My click event: 我的点击事件:

$('.day').on('click', function() {
  getTimes($(this).text());
});

My first function that does the ajax (this is where the selector returns a value): 我的第一个执行ajax的函数(这是选择器返回值的地方):

function getTimes(selector) {
  console.log(selector);
  var eventDate = selector;

  getJSONP(CAL_API_URL).then(function(data) {
    data = _.valuesIn(data)[5][0];
    return data;
  }).then(appendTimes(eventDate));
}

My 2nd function that renders the HTML (here the selector is undefined): 我的第二个函数呈现HTML(这里选择器是未定义的):

function appendTimes(dates, selector) {
  console.log(selector);

  _.forEach(dates, function(k, v) {
    // Returns each event

    _.forEach(k, function(k2, v2) {
      // Returns multiple objects for each event

      var availableTimes = k2.startTime + " (" + k2.open + " spots left!)",
          timeId = k2.id;
      $('.timeslot select').append('<option data-id="' + timeId +'">' + availableTimes + '</option>');
    });
  });
}

In this case, if you switched appendTimes to only accept selector and have it return a function, the returned function will get called when data is available. 在这种情况下,如果您将appendTimes切换为仅接受selector并让它返回一个函数,则在数据可用时将调用返回的函数。

function appendTimes(selector) {
  console.log(selector);
  return function (datas) {
    _.forEach(dates, function(k, v) {
      // Returns each event

      _.forEach(k, function(k2, v2) {
        // Returns multiple objects for each event

        var availableTimes = k2.startTime + " (" + k2.open + " spots left!)",
            timeId = k2.id;
        $('.timeslot select').append('<option data-id="' + timeId +'">' + availableTimes + '</option>');
      });
    });
  }
}

You are calling the method, not assigning a reference to it 您正在调用该方法,而不是为其分配引用

}).then(appendTimes(eventDate));

needs to use a closure or binding 需要使用闭包或绑定

}).then(function() { appendTimes(eventDate) });

also you can not return from a promise so the return data is useless. 你也不能从承诺中返回,所以return data是无用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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