why cant I return data from $.post (jquery)

前端 未结 6 2448
南旧
南旧 2020-12-19 05:39

I must be making a silly mistake but I cannot return the data I get from a $.post function and store it in a variable, not only that, I cannot return ANYTHING from within th

6条回答
  •  遥遥无期
    2020-12-19 05:59

    $.post is a asynchronous function. The control from function will immediately return after it run post but the response from post may be received later.

    So what you can do is instead of return, use a call back function and define callback function outside.

    say,

    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
    }
    

提交回复
热议问题