[英]Modern or Non-deprecated way to detect flash player with js/jquery?
[英]What's a non-deprecated way of doing a synchronous ajax call using jQuery?
我有一个小的javascript函数,唯一的目的是调用脚本从数据库中获取一些数据,以便它可以被客户端的其他函数使用。
我正在使用jQuery调用来获取数据,但是为了让我将对象从成功函数范围中传出,我需要将异步关闭,这会引发弃用警告。
我的函数目前按预期工作,但我想使用一个不被弃用的方法。 这是我的功能:
function getData(ID) {
var Data = {};
$.ajax({
url: 'script',
method: 'POST',
dataType: 'json',
async: false,
data: {action: 'get', id: ID },
success: function(response) {
Data = response;
})
});
return Data;
}
我出于隐私原因更改了变量名称,如果它们含糊不清,请道歉。
同样为什么同步呼叫被认为对最终用户体验有害?
由于AJAX调用是asynchronous
,因此总是会得到空白对象( {}
)作为响应。
有2种方法。
async:false
function getData(ID) {
return $.ajax({
url: 'script',
method: 'POST',
dataType: 'json',
//async: true, //default async call
data: {action: 'get', id: ID },
success: function(response) {
//Data = response;
})
});
}
$.when(getData(YOUR_ID)).done(function(response){
//access response data here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.