can't get answer of ajax with Javascript -


this question has answer here:

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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -