Why can not I return data from $ .post (jquery)

I have to make a stupid mistake, but I cannot return the data received from the $ .post function and store it in a variable, and not only I can not return ANYTHING from this function. Example:

function test(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ return(data) }); } var foo = test() alert(foo) 

he says foo is undefined. But do it even further, even when I do it:

 function test(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ var bar = "bar" return(bar) }); } var foo = test() alert(foo) 

this STILL says foo is undefined ... I have to do something wrong or misunderstand something. Maybe someone can help.

thanks

+4
source share
6 answers

$. post is an asynchronous function. The control from the function will immediately return after the message is started, but the response to the message may be received later.

So what you can do, instead of returning, use the callback function and define the callback function from the outside.

eg,

 function test(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ my_function(data) }); } function my_function(data){ // you can operate on data here } 
+10
source

You are not returning anything from post() . What you have inside function(data, status) {} is actually a callback and does not return the result to the post() method, as you think.

Read this article for more information.

+3
source

jQuery post() by default Asynchronous means that you can never return a value from such a function.

To send jQuery Docs

async (default: true)

Type: Boolean

By default, all requests are sent asynchronously (i.e., this is set to true by default). If you need synchronous requests, set this parameter to false.

You will need to provide a callback function to update the value.

eg. A very simple example.

 var foo; test(); function test(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ var bar = "bar" foo = bar; // assign foo }); } } 

Alternatively, you can see how to change jQuery Ajax to Synchronous . Take a look at this post here. How can I get jQuery to execute a synchronous rather than asynchronous Ajax request?

+3
source

Try using .done () to execute the returned data. This should ensure that it receives the data and does not set your variable or warning data before it finishes.

 $.post("demo_test_post.asp", { name: "Donald Duck", city: "Duckburg" }) .done(function(data) { alert("Data Loaded: " + data); //Or return data; }); 
+2
source

jQuery AJAX requests are executed asynchronously, which means the request is running, and the $.post() method returns more or less immediately. The request is processed by the browser in the background. When the query is complete, jQuery calls the callback function you provided in $.post() . You can use async: false to tell jQuery to block until the request is completed, but this is not recommended, as this will almost certainly lead to terrible performance and poor user experience.

Instead, you should write your code so that the logic that depends on the AJAX response is triggered by a callback function. Sort of:

 function handleAjaxResponse(responseData) { // do something with the response. } function doAjax() { $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data, status){ handleAjaxResponse(data); }); } doAjax(); 
+1
source

You use the more streamlined .post() method. This is even more useful for working with (IMHO) and somewhat less powerful than the larger form, $.ajax() .

Personally, I found the .ajax() method preferable because the added structure makes formatting easier. In addition, you can do more with .ajax (there are more options, such as turning off asynch, which is sometimes very useful when you want to defer processing until data is returned).

Here is a simple example of using the full $.ajax() method. Check out the success function below - this is where you can use the data sent back from another PHP file!

+1
source

Source: https://habr.com/ru/post/1485078/


All Articles