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
Add a ment  | 

1 Answer 1

Reset to default 5

As AJAX call is asynchronous, you will always get blank object ({}) in response.

There are 2 approach.

  1. You can do async:false
  2. 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