Javascript 网站未等待同步web服务的结果
我在我的网站中调用了同步web服务,但不需要等待它们的结果 我在函数loadLayout中校准函数loadExtLayout,然后在网站的其他函数中调用函数loadLayoutJavascript 网站未等待同步web服务的结果,javascript,ajax,Javascript,Ajax,我在我的网站中调用了同步web服务,但不需要等待它们的结果 我在函数loadLayout中校准函数loadExtLayout,然后在网站的其他函数中调用函数loadLayout HTTPRequestService.prototype.loadExtLayout = function(pathToLoad){ logManager.IHM_LOG_INFO("BEGIN HTTPRequestService loadExtLayout call pathToLoad="+
HTTPRequestService.prototype.loadExtLayout = function(pathToLoad){
logManager.IHM_LOG_INFO("BEGIN HTTPRequestService loadExtLayout call pathToLoad="+JSON.stringify(pathToLoad));
var loadResult = null;
$.ajax({
async:false,
method: "GET",
url: pathToLoad
}).done(function(result){
loadResult = result;
}).fail(function(jqXHR, textStatus){
loadResult = null;
logManager.IHM_LOG_ERROR(new Error().stack+": "+"Error loading layout : " + pathToLoad + " (" + textStatus + ")\n");
});
logManager.IHM_LOG_INFO("END HTTPRequestService loadExtLayout call");
return loadResult;
}
GenericLayoutController.prototype.loadLayout = function(layoutName){
logManager.IHM_LOG_INFO("BEGIN loadLayout");
var loadResult = false;
var layoutContent = null;
try {
var httpService = new HTTPRequestService(this.AppId);
if(httpService != null){
layoutContent = httpService.loadExtLayout(layoutName);
console.log("layoutContent :" + layoutContent);
if ((layoutContent != null) && ($("#window_"+ this.AppId + "_" + this.WndId).attr("patternname") == this.patternName)) {
$("#window_"+ this.AppId + "_" + this.WndId).empty();
$("#window_"+ this.AppId + "_" + this.WndId).html(layoutContent);
loadResult = true;
} else if( layoutContent == null ){
logManager.IHM_LOG_ERROR("Error loading layout !");
}
} else {
logManager.IHM_LOG_ERROR("Error unable to create HTTPRequestService object : httpService is null !");
}
} catch(e) {
loadResult = false;
logManager.IHM_LOG_ERROR(new Error().stack+": "+e+"\n");
}
logManager.IHM_LOG_INFO("END loadLayout");
return loadResult;
}
如果您使用ajax参数'async:false',则必须使用回调(成功)而不是承诺,因为'then'异步工作,并在承诺从服务器检索数据之前执行'return'
let data = null;
$.ajax({
async:false,
method: "GET",
url: pathToLoad,
success:(response)=>{data = response}
});
return data;
或者可以异步执行
HTTPRequestService.prototype.loadExtLayout = function(pathToLoad){
logManager.IHM_LOG_INFO("BEGIN HTTPRequestService loadExtLayout call pathToLoad="+JSON.stringify(pathToLoad));
let loadResult = new Promise((resolve,reject)=>{
$.ajax({
async:false,
method: "GET",
url: pathToLoad
}).done(function(result){
resolve(result);
}).fail(function(jqXHR, textStatus){
reject(textStatus);
loadResult = null;
logManager.IHM_LOG_ERROR(new Error().stack+": "+"Error loading layout : " + pathToLoad + " (" + textStatus + ")\n");
});
});
logManager.IHM_LOG_INFO("END HTTPRequestService loadExtLayout call");
return loadResult;
}
这对我不起作用,我需要所有的治疗同步,等待web服务的结果。是否有强制同步执行的库?