Ajax is meant to be async
Ajax is meant to be async so you can't return the response inside the function itself. You need to create a callback function. Sync ajax crashes/locks up the browsers.
Here are some ajax examples based on xhr2 witch should be the standard now.
it's supported by all new browsers including ie10,android & ios.
Simple get ajax function
//url,callback
function ajax(a,b,c){
c=new XMLHttpRequest;
c.onload=b;
c.open('get',a);
c.send()
}
Usage
ajax(url,callback);
How to access the return value inside the callback
function callback(e){
e.target.response
this.response
c.response// if inline inside the ajax function.
}
Complex ajax function
//url,callback,type,FormData,uploadFunc,downloadFunc
function ajax(a,b,e,d,f,g,c){
c=new XMLHttpRequest;
!f||(c.upload.onprogress=f);
!g||(c.onprogress=g);
c.onload=b;
c.open(e||'get',a);
c.send(d||null)
}
Usage
var fd=new FormData();
fd.append('act','get_conv_data');
fd.append('click_id',click_id);
fd.append('server',server);
ajax(ajaxUrl,callback,'post',fd);
function callback(){
//when the request finishes do stuff with the response.
alert(this.response)
}
or
html
<form id="form">
<input name="server" value="serverBLABLA">
<input name="click_id" value="click_idBLABLA">
<input name="act" value="get_conv_data">
</form>
js
ajax(ajaxUrl,callback,'post',new FormData(document.getElementById('form')));
function callback(){
//when the request finishes do stuff with the response.
alert(this.response)
}
More info about this function
https://stackoverflow.com/a/18309057/2450730