Getting undefined in javascript when calling ajax

function get_request(url) { var request = new getXMLObject(); request.onreadystatechange = function () { if (request.readyState == 4) { alert(request.responseText); var data = eval('(' + request.responseText + ')'); alert(data); return data; } } request.open("GET", url, true); //alert(document.getElementById('energy').innerHTML); request.send(); } function loadjobs() { var url = "loadjobs.php?tab=1&id=1111"; //var data= //alert(check()); alert(get_request(url)); //alert(data); } 

When I get the data in json format ... I am gettin NULL in alert(get_request(url)); while I get alert(data);

help me

0
source share
4 answers

This is because the request is in asynchronous mode. The get_request(url) function returns something and therefore the value is null (although I think it should be undefined, not null).

The onreadystatechange function onreadystatechange called later at the time when the AJAX request is completed and the data is returned from the server, and therefore the warning works there.

+3
source

This is a misunderstanding of how AJAX works. Ajax is asynchronous. The onreadystatechange function onreadystatechange called after loadjobs() . The "return path" you specify will never work. get_request() will never be able to return the retrieved value.

You have two options. Or to make the script synchronous - this can be done, but it is not recommended, since it can freeze the browser.

Or, better, handle everything you need to do in the onreadystatechange .

0
source

The problem is that Ajax is requesting asynchronously . Therefore, you cannot immediately return the data. The way you should do this is to specify a callback function that will process the response data.

 function handleJSON( data ) { // ... // do whatever you want to do with the data } ajax( "url/file.php?param=value", handleJSON ); //////////////////////////////////////////////////////////////////////////////// function getXmlHttpObject() { var xmlHttp; try { xmlHttp = new XMLHttpRequest(); } catch (e) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function ajax(url, onSuccess, onError) { var xmlHttp = getXmlHttpObject(); xmlHttp.onreadystatechange = function () { if (this.readyState == 4) { // onError if (this.status != 200) { if (typeof onError == 'function') { onError(this.responseText); } } // onSuccess else if (typeof onSuccess == 'function') { onSuccess(this.responseText); } } }; xmlHttp.open("GET", url, true); xmlHttp.send(null); return xmlHttp; }​ 
0
source

Well, this is an asynchronous call. You will get the request data after get_request . This means that your request.onreadystatechange = function () will be executed long after the completion of alert(get_request(url)); . This means that get_request will not be able to return data from an AJAX call. That you have a request.onreadystatechange callback function to execute code with undefined later when you receive a response.

0
source

Source: https://habr.com/ru/post/1314453/


All Articles