admin管理员组文章数量:1399155
I've got a small javascript function that's only purpose is to call a script to get some data from the database so it can be used by other functions on the client side.
I'm using a jQuery call to get the data but for me to pass the object out of the success functions scope I need to turn asynchronous off which raises a deprecation warning.
My function works as intended currently but I'd like to use a method that isn't deprecated. Here is my function:
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;
}
I've changed the variable names for privacy reasons so apologies if they're vague.
Also why is synchronous calls considered harmful to the end users experience?
I've got a small javascript function that's only purpose is to call a script to get some data from the database so it can be used by other functions on the client side.
I'm using a jQuery call to get the data but for me to pass the object out of the success functions scope I need to turn asynchronous off which raises a deprecation warning.
My function works as intended currently but I'd like to use a method that isn't deprecated. Here is my function:
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;
}
I've changed the variable names for privacy reasons so apologies if they're vague.
Also why is synchronous calls considered harmful to the end users experience?
Share Improve this question asked Jun 20, 2017 at 4:56 FashimFashim 1643 silver badges10 bronze badges 4- @Keatinge Why do you say that? It's currently returning the object as intended every time. – Fashim Commented Jun 20, 2017 at 5:01
-
@Keatinge I think it'll work because of the
async: false
option that's been passed into$.ajax()
– Chitharanjan Das Commented Jun 20, 2017 at 5:01 - 1 You might want to take a look at How do I return the response from an asynchronous call?. There I am also saying about sync calls: "Why is it bad do you ask? JavaScript runs in the UI thread of the browser and any long running process will lock the UI, making it unresponsive. Additionally, there is an upper limit on the execution time for JavaScript and the browser will ask the user whether to continue the execution or not." – Felix Kling Commented Jun 20, 2017 at 5:11
- To answer your question though: There is no non-deprecated way to make a synchronous network request. Synchronous is deprecated. – Felix Kling Commented Jun 20, 2017 at 5:13
1 Answer
Reset to default 5As AJAX call is asynchronous
, you will always get blank object ({}
) in response.
There are 2 approach.
- You can do
async:false
- To get response returned in AJAX call try like below code. Which wait for response from server.
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
});
本文标签: javascriptWhat39s a nondeprecated way of doing a synchronous ajax call using jQueryStack Overflow
版权声明:本文标题:javascript - What's a non-deprecated way of doing a synchronous ajax call using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744211925a2595460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论