JS中的Ajax aysnc请求 [英] Ajax aysnc request in JS

查看:121
本文介绍了JS中的Ajax aysnc请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用此函数从数据库中获取一些数据并将结果存储在类stripLayout中。但是问题是在第一次运行期间类仍然是空的,所以我引入了async:false并解决了问题。



但我读到这不是正确的方法,而且可以使用'promise'。当我尝试做这样的改变时,我找不到让这段代码工作的方法。有人可以帮帮我吗?



 function getStripLayoutsDetails(){
$ .ajax('admnStripLayouts_getStripLayoutDetails.php',
{
async:false,
类型:'POST',
数据:{
LayoutName:$('#aSL_stripLayoutsList')。val()
} ,
成功:函数(结果){

stripLayout = {
layoutName:result.stripLayoutDetails [0] .LayoutName,
version:result.stripLayoutDetails [0] .Version
};

if(result.status ==Error){
displayError(result.detail,'Error Retrieving Strip Layout Details','critical') ;
返回;
}
},
错误:函数(resp,status,xhr){
displayError('对服务器的有效值查找请求失败','查找错误','严重');
}
});
}





我的尝试:



试过这个改变,但这与没有异步时的行为类似:false



 function getStripLayoutsDetails(){
$ .ajax(
{url:/ admnStripLayouts_getStripLayoutDetails.php,
类型:'POST',
数据:{
LayoutName:$('#aSL_stripLayoutsList' ).val()
}
})
.done(function(getStripLayoutsDetails){
stripLayout = {
layoutName:getStripLayoutsDetails.stripLayoutDetails [0] .LayoutName,
version:getStripLayoutsDetails.stripLayoutDetails [0] .Version
};
})。fail(function(e){
displayError('对服务器的有效值查找请求失败', 'Lookup Error','critical');

})。always(function(){});
}

解决方案

.ajax('admnStripLayouts_getStripLayoutDetails.php',
{
async:false,
类型:'POST',
data:{
LayoutName:


('#aSL_stripLayoutsList')。val()
},
成功:函数(结果){

stripLayout = {
layoutName:result.stripLayoutDetails [0] .LayoutName,
version:result。 stripLayoutDetails [0] .Version
};

if(result.status ==Error){
displayError(result.detail,'Error Retrieving Strip Layout Details', 'critical');
return;
}
},
错误:function(resp,status,xhr){
displayError('对服务器的有效值查找请求失败','查找错误','严重');
}
});
}





我的尝试:



试过这个改变,但这与没有异步时的行为类似:false



 function getStripLayoutsDetails(){


.ajax(
{url:/ admnStripLayouts_getStripLayoutDetails.php,
类型:'POST',
数据:{
LayoutName:

Hello,

I am using this function to acquire some data from a DB and store the result in a class stripLayout. However the problem is that during the first run the class remains empty, so I introduced the async: false and solved the problem.

But I was reading that this is not the right way to do it, and 'promise' can be used instead. I could not find a way that makes this code work when I tried to do such a change. Can anyone help me out please?

function getStripLayoutsDetails(){
	$.ajax( 'admnStripLayouts_getStripLayoutDetails.php',
	{
		async: false,
		type: 'POST',
		data: {					
			LayoutName: $('#aSL_stripLayoutsList').val()					
		},
		success: function (result) {

		stripLayout = {
			layoutName:result.stripLayoutDetails[0].LayoutName,
			version:result.stripLayoutDetails[0].Version
		};				
							
		if(result.status == "Error"){
			displayError(result.detail, 'Error Retrieving Strip Layout Details', 'critical');
			return;
			}					
		},							
		error: function (resp, status, xhr) {
			displayError('Valid Value lookup request to server failed', 'Lookup Error', 'critical');
		}
	}); 
}



What I have tried:

Tried this change, however this is behaving similar to when not having async: false

function getStripLayoutsDetails(){
    $.ajax(
        { url: "/admnStripLayouts_getStripLayoutDetails.php" ,
            type: 'POST',
            data: {
                LayoutName: $('#aSL_stripLayoutsList').val()
            }
    })
    .done(function(getStripLayoutsDetails) {
        stripLayout = {
            layoutName:getStripLayoutsDetails.stripLayoutDetails[0].LayoutName,
            version:getStripLayoutsDetails.stripLayoutDetails[0].Version
    };
}).fail(function(e) {
    displayError('Valid Value lookup request to server failed', 'Lookup Error', 'critical');

    }).always(function() {} );
}

解决方案

.ajax( 'admnStripLayouts_getStripLayoutDetails.php', { async: false, type: 'POST', data: { LayoutName:


('#aSL_stripLayoutsList').val() }, success: function (result) { stripLayout = { layoutName:result.stripLayoutDetails[0].LayoutName, version:result.stripLayoutDetails[0].Version }; if(result.status == "Error"){ displayError(result.detail, 'Error Retrieving Strip Layout Details', 'critical'); return; } }, error: function (resp, status, xhr) { displayError('Valid Value lookup request to server failed', 'Lookup Error', 'critical'); } }); }



What I have tried:

Tried this change, however this is behaving similar to when not having async: false

function getStripLayoutsDetails(){


.ajax( { url: "/admnStripLayouts_getStripLayoutDetails.php" , type: 'POST', data: { LayoutName:


这篇关于JS中的Ajax aysnc请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆