How to return a value from a function that calls $.getJSON?

后端 未结 3 1167
青春惊慌失措
青春惊慌失措 2020-12-01 15:02
function lookupRemote(searchTerm)
{
   var defaultReturnValue = 1010;
   var returnValue = defaultReturnValue;

   $.getJSON(remote, function(data)
   {
      if (da         


        
3条回答
  •  北海茫月
    2020-12-01 15:38

    The function you pass to getJSON is run when the response to the HTTP request arrives which is not immediately.

    The return statement executes before the response, so the variable hasn't yet been set.

    Have your callback function do what needs doing with the data. Don't try to return it.

提交回复
热议问题