Javascript ajax异步的替代方案:false
在ajax完成之前中断属性激发。我找到的唯一解决方案是使“async:false”。但它已被弃用。有没有更好的选项可以等到ajax完成而不将async设置为falseJavascript ajax异步的替代方案:false,javascript,jquery,ajax,Javascript,Jquery,Ajax,在ajax完成之前中断属性激发。我找到的唯一解决方案是使“async:false”。但它已被弃用。有没有更好的选项可以等到ajax完成而不将async设置为false $.getJSON(jsonUrl, function(data) { var type, json, options; var globalData = data; $.each(globalData.properties, function(key, value) { switch(value.type
$.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)});
$.getJSON(jsonUrl,函数(数据){
var类型、json、options;
var globalData=数据;
$.each(globalData.properties,函数(键,值){
开关(值.类型){
大小写“string”:
类型=“”
打破
案例“int”:
类型=“”
打破
“十进制”大小写:
类型=“”
打破
案例“日期”:
类型=“”
打破
“布尔”大小写:
类型=“”
打破
案例“json”:
json='./assets/json/'+value.json_name+'.json'
$.ajax({
async:false,
url:json,
成功:功能(数据){
$。每个(数据、功能(索引、项目){
选项+=''+项值+''
})
类型=“”+选项+“”;
}
});
打破
}
listItem+=''+
'' +
'' +
value.description+':'+
'' +
'' +
类型+
'' +
'' +
“ ”;
类型=“”;
})
form.innerHTML+=listItem;
$('.content').append(form)};
解决此问题的最佳方法是将中断或操作放在ajax请求的成功回调中,此外,您可以在发送ajax请求的回调之前在中使用布尔值集,并在布尔值仍然为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)});
$.getJSON(jsonUrl,函数(数据){
var类型、json、options;
var globalData=数据;
$.each(globalData.properties,函数(键,值){
var isBusy=false;
开关(值.类型){
大小写“string”:
类型=“”
打破
案例“int”:
类型=“”
打破
“十进制”大小写:
类型=“”
打破
案例“日期”:
类型=“”
打破
“布尔”大小写:
类型=“”
打破
案例“json”:
json='./assets/json/'+value.json_name+'.json'
$.ajax({
async:false,
url:json,
beforeSend:function(){
isBusy=true;
},
成功:功能(数据){
isBusy=false;
$。每个(数据、功能(索引、项目){
选项+=''+项值+''
})
类型=“”+选项+“”;
}
});
趁(忙){}
打破
}
listItem+=''+
'' +
'' +
value.description+':'+
'' +
'' +
类型+
'' +
'' +
“ ”;
类型=“”;
})
form.innerHTML+=listItem;
$('.content').append(form)};
但是一旦它再次打破了AJAX请求的全部意义,您最好在加载页面之前准备好数据,然后显示它
老实说,这个解决方案不是最好的,正如Rory在评论中说的那样,我会同意promise。解决方案是使用promises,但是另一个问题是,您正在创建多个select
元素,这些元素具有相同的id
attributeoh,id将被删除)在这种情况下,我如何使用promise?我尝试了一些变体,但它们不起作用…可能重复我将中断置于回调或承诺中时它会抛出错误(您应该将中断后尝试执行的操作添加到成功中,它无论如何都会中断它是异步的)