can't get answer of ajax with Javascript -
this question has answer here:
- how return response asynchronous call? 21 answers
i have this:
var = makerequest(url); function makerequest(url) { var http_request = false; if (window.xmlhttprequest) { http_request = new xmlhttprequest(); if (http_request.overridemimetype) { http_request.overridemimetype('text/xml'); } } else if (window.activexobject) { try { http_request = new activexobject("msxml2.xmlhttp"); } catch (e) { try { http_request = new activexobject("microsoft.xmlhttp"); } catch (e) {} } } if (!http_request) { console.log('falla :( no es posible crear una instancia xmlhttp'); return false; } http_request.onreadystatechange = alertcontents; http_request.open('get', url, true); http_request.send(null); function alertcontents() { if (http_request.readystate == 4) { if (http_request.status == 200) { //console.log(http_request.responsetext); //this show string result //return http_request.responsetext; //this not return nothing //var = http_request.responsetext; // return undefined variable } else { console.log('hubo problemas con la peticiĆ³n.'); } } } }
look under "if (http_request.status == 200)" 3 things prove , nothing.
i don't know how out of function result of call. please me
your alertcontents() function executed asynchronously. cannot return value function. can declare global variable , assign reponsetext value , process it.
var result ; function alertcontents() { if (http_request.readystate == 4) { if (http_request.status == 200) { result = http_request.responsetext ; process(result) ; // guaranteed there response } } } process(result); // not guarantee result have // returned value (response) , may result in failure
Comments
Post a Comment