Make AJAX “get” function synchronous / how to get the result?

后端 未结 3 1880
天涯浪人
天涯浪人 2021-01-07 23:08

I\'m experiencing a problem of $.get function. The url contains JSON

my code:

 xyz = null

    $.get(\'http://www.someurl.c         


        
3条回答
  •  余生分开走
    2021-01-07 23:43

    get is a shortcut. You can do the same, but synchronous, using:

    var xyz = null
    
    
    $.ajax({ url: 'http://www.someurl.com/123=json', 
             async: false,
             dataType: 'json',
             success: function(data) {
                  xyz = data.positions[0].latitude;
                }
            });
    
    
    alert(xyz);
    

    You'll have to declare the xyz variable before the ajax call, though.

提交回复
热议问题