I need to use a variable outside its function. I already read older questions, but I still can't find a proper solution.
Here's the code:
<script>
var warehouseAddress;
$(document).ready(function(){
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/users/findWarehouseAddress",
success:function(data){
warehouseAddress = data;
}
});
alert(warehouseAddress);
});
var warehouse = warehouseAddress;
</script>
The alert gives me "undefined". I need to get that variable not only inside the alert, but also inside the other "warehouse" value that is outside the ready function.
Sorry if it's a duplicate, but as I said I couldn't make it with the answers I read.
EDIT: For the sake of completeness I describe the details, replacing the alerts with the read functions I have to call.
As I said in comments, I'm actually using Google Maps APIs and their functions. What I have to do is passing the variable outside the $(document).ready function, because I need to use it in an external script.
The problem is that the external js function that I have to call is a Google Maps function (initialize()) that loads as the window loads, so I have to find a way to call that function after the $ready function.
var warehouse = warehouseLatare executed earlier than the AJAX success method. And yourvar warehouse = ...is also outside the document ready which is giving even more problems. Can you not call your final function within the success method of AJAX?