ajax async 的替代方法:false



在 Ajax 完成之前中断属性触发。我找到的唯一解决方案是使"异步:假"。但它已被弃用。有没有更好的选择等到 ajax 完成而不将异步设置为 false?

$.getJSON(jsonUrl, function(data) {
  var type, json, options;
  var globalData = data;
  $.each(globalData.properties, function(key, value) {
    switch(value.type) {
      case 'string':
        type = '<input type="text">'
        break;
      case 'int':
        type = '<input type="number" min="0">'
        break;
      case 'decimal':
        type = '<input type="number" min="0" step="0.01">'
        break;
      case 'date':
        type = '<input type="date">'
        break;
      case 'boolean':
        type = '<input type="checkbox">'
        break;
      case 'json':
        json = './assets/json/' + value.json_name + '.json'
        $.ajax({
          async: false,
          url: json,
          success: function(data) {
            $.each(data, function(index, item) {
              options += '<option value=' + item.key + '>' + item.value + '</option>'
            })
            type = '<select id="issue-form"><option disabled selected value></option>' + options + '</select>';
          }
        });
        break;
    }

    listItem += '<li class="questionnaire_item">' + 
                  '<label>' + 
                    '<div class="question">' + 
                      value.description + ':' + 
                    '</div>' + 
                    '<div class="input">' + 
                      type + 
                    '</div>' + 
                  '</label>' +
                '</li>';
    type = '';
  })
  form.innerHTML += listItem;
  $('.content').append(form)});

解决此问题的最佳方法是将中断或操作放在 ajax 请求的成功回调中,此外,您还可以在 ajax 请求的beforeSend回调中使用布尔集,并在布尔值仍然为 false 时等待:

$.getJSON(jsonUrl, function(data) {
var type, json, options;
var globalData = data;
  $.each(globalData.properties, function(key, value) {
  var isBusy = false;
    switch(value.type) {
      case 'string':
        type = '<input type="text">'
        break;
      case 'int':
        type = '<input type="number" min="0">'
        break;
      case 'decimal':
        type = '<input type="number" min="0" step="0.01">'
        break;
      case 'date':
        type = '<input type="date">'
        break;
      case 'boolean':
        type = '<input type="checkbox">'
        break;
      case 'json':
        json = './assets/json/' + value.json_name + '.json'
        $.ajax({
          async: false,
          url: json,
          beforeSend: function(){
            isBusy = true;
          },    
          success: function(data) {
            isBusy = false;
            $.each(data, function(index, item) {
              options += '<option value=' + item.key + '>' + item.value + '</option>'
            })
            type = '<select id="issue-form"><option disabled selected value></option>' + options + '</select>';
          }
        });
            while(isBusy) {}
        break;
    }

listItem += '<li class="questionnaire_item">' + 
              '<label>' + 
                '<div class="question">' + 
                  value.description + ':' + 
                '</div>' + 
                '<div class="input">' + 
                  type + 
                '</div>' + 
              '</label>' +
            '</li>';
type = '';
  })
  form.innerHTML += listItem;
  $('.content').append(form)});

但是它再次打破了拥有AJAX请求的全部意义,您最好在加载页面之前准备数据,然后显示它。

老实说,这个解决方案不是最好的解决方案,正如罗里在评论中所说的那样,我会承诺。

最新更新