javascript - How to make it work with Asynchronous -
this question has answer here:
i want able results asynchronous method. reason works in other method. trying return response of callback method. can
ajax.getresults();
instead of:
ajax.getresults(function(response){ // code here... });
here code...
function ajax(method, destination, sendtype){ this.method=method; this.destination=destination; this.sendtype=sendtype; // creates xmlhttp object this.create=function(){ var xmlhttp; if(window.xmlhttprequest){ xmlhttp=new xmlhttprequest(); }else{ xmlhttp=new activexobject("microsoft.xmlhttp"); } return xmlhttp; } this.request=function(callback){ var xmlhttp=this.create(); xmlhttp.onreadystatechange=function(){ if(xmlhttp.readystate==4 && xmlhttp.status==200){ // response callback(xmlhttp.responsetext); } } xmlhttp.open(this.method, this.destination, this.sendtype); xmlhttp.send(); } } // trying results this.request() ajax.prototype.getresults=function(){ var result; this.request(function(response){ result=response; }); return result; } function ajaxtest(){ var ajax=new ajax('post', 'echo.php', true); alert(ajax.getresults()); }
Comments
Post a Comment