jquery ajax return: undefined

后端 未结 5 1967
暗喜
暗喜 2020-12-06 11:30

I\'m sure you know this problem, I\'m still trying to solve it for few days. I\'ve tried lots of stuff but no one worked:

Here is the code

function l         


        
5条回答
  •  庸人自扰
    2020-12-06 11:53

    Correctly stated that you can't really return from the Success, however, if you are doing what I think you are doing, and that is to grab the data and return it and assign it to something in another area IE

    var obj = lobbyLeader();

    Then you can just set the function to async:false and return the obj outside of the Success after the code has finished running. example

    function lobbyLeader() {
    var obj;
      $.ajax({
        async:false;
        data: {"id": 1, "request": "lobbyinfo", "method": "read"},
        url: 'api.php',
        dataType: 'json',
        success: function(data){
          obj= JSON.parse(data);
       }
     });
        return obj;
    }
    

    This way the script stops to wait for success and then sets and returns.

提交回复
热议问题