I am making a synchronous REST API call in the main thread. jQuery (or browser?) complains. But I don't know how to code it asynchronously, or at least the code is less confusing to read as it stands.
This is a simplified version of what I'm doing:
<html>
<head>
<script type="text/javascript" src="/js/jquery.min.js" ></script>
</head>
<body>
<script type="text/javascript">
function get_foobar(val1, val2){
$.ajax({
url: 'http://localhost:8000/api/' + va1 + '/' + val2,
success: function (data) {
let my_object = $.parseJSON(data)
//console.log(data);
console.log(my_object);
let params = my_object['params'];
let blob = my_object['the_thing'];
var foobar = new FooBar(var1, var2, params, blob);
return foobar; // <- object created here ...
},
async: false
});
}
$(document.ready(function(){
foobar = get_foobar($('#field1').val, $('#field2').val());
console.log(foobar); // <- undefined here ... wtf?
});
</script>
</body>
</html>
Why is the function not returning the foobar
object that is created?