I have a example fetch data through ajax and do something. Condition is this function can reuseable. Can call anywhere.
<script type="text/javascript">
function getData(value1,value2){
$.ajax({
url: URL,
data:{
email:value1,
age:value2
},
async:false,
dataType: "json",
type: "get",
success: function(data){
var result;
result = data.result;
console.log("result:"+result);
},
error: function(){
}
});
};
var data;
data = getData("email","age");
console.log("data:"+data);
</script>
When i call ajax, result return later so data empty.I try use async false but just ok inside ajax.
How to do do javascript wait for the response and not execute any more, get the response and then continue executing. Set timeout is not good idea because it causing problems about server response speed on different servers and server to different client.