在调用ajax时在javascript中未定义

时间:2022-10-17 09:15:26
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 m getting data in json format...i am gettin NULL in alert(get_request(url)); while i m getting in alert(data);

当我得到json格式的数据…我在alert(get_request(url)中设置为NULL;当我进入警报(数据);

Help me

帮助我

4 个解决方案

#1


3  

This is because the request in asynchronous . The get_request(url) function does to return anything and hence the null ( although I think it should be undefined and not null ) .

这是因为请求是异步的。get_request(url)函数返回任何东西,因此返回null(尽管我认为它应该是无定义的,不是null)。

The onreadystatechange function gets called later in the time , when the AJAX request has been completed and the data is returned from the server and hence the alert there works .

onreadystatechange函数将在稍后调用,此时AJAX请求已经完成,数据将从服务器返回,因此警报将正常工作。

#2


0  

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

这是对AJAX工作方式的误解。Ajax是异步的。onreadystatechange函数将在loadjobs()之后调用。您指定的“返回路径”无法工作。get_request()永远不能返回提取的值。

You have two options. Either make the script synchronous - this can be done but is not recommended because it can freeze the browser.

你有两个选择。要么使脚本同步——可以这样做,但不建议这样做,因为它可能会冻结浏览器。

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

或者,更好的做法是,在onreadystatechange回调中处理所有需要执行的操作。

#3


0  

Well, it's an asynchronous call. You will receive the data of request your after get_request has already returned. That means your request.onreadystatechange = function () will be executed long after alert(get_request(url)); is already finished. This means get_request will not be able to return any data from the AJAX call. That's what you have the request.onreadystatechange callback function for, to execute code at an undefined later time when you received the response.

这是一个异步调用。您将在get_request已经返回后接收请求的数据。这意味着你的请求。onreadystatechange = function()将在警报(get_request(url))后长时间执行;已经完成了。这意味着get_request不能从AJAX调用返回任何数据。这就是你的要求。onreadystatechange回调函数,在以后收到响应时执行未定义的代码。

#4


0  

The problem is that Ajax requests work asynchronously. So you can't return the data right away. The way you should do it is to specify a callback function which will handle the response data.

问题是Ajax请求是异步工作的。所以你不能马上返回数据。您应该这样做的方法是指定一个回调函数来处理响应数据。

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;
}​

#1


3  

This is because the request in asynchronous . The get_request(url) function does to return anything and hence the null ( although I think it should be undefined and not null ) .

这是因为请求是异步的。get_request(url)函数返回任何东西,因此返回null(尽管我认为它应该是无定义的,不是null)。

The onreadystatechange function gets called later in the time , when the AJAX request has been completed and the data is returned from the server and hence the alert there works .

onreadystatechange函数将在稍后调用,此时AJAX请求已经完成,数据将从服务器返回,因此警报将正常工作。

#2


0  

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

这是对AJAX工作方式的误解。Ajax是异步的。onreadystatechange函数将在loadjobs()之后调用。您指定的“返回路径”无法工作。get_request()永远不能返回提取的值。

You have two options. Either make the script synchronous - this can be done but is not recommended because it can freeze the browser.

你有两个选择。要么使脚本同步——可以这样做,但不建议这样做,因为它可能会冻结浏览器。

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

或者,更好的做法是,在onreadystatechange回调中处理所有需要执行的操作。

#3


0  

Well, it's an asynchronous call. You will receive the data of request your after get_request has already returned. That means your request.onreadystatechange = function () will be executed long after alert(get_request(url)); is already finished. This means get_request will not be able to return any data from the AJAX call. That's what you have the request.onreadystatechange callback function for, to execute code at an undefined later time when you received the response.

这是一个异步调用。您将在get_request已经返回后接收请求的数据。这意味着你的请求。onreadystatechange = function()将在警报(get_request(url))后长时间执行;已经完成了。这意味着get_request不能从AJAX调用返回任何数据。这就是你的要求。onreadystatechange回调函数,在以后收到响应时执行未定义的代码。

#4


0  

The problem is that Ajax requests work asynchronously. So you can't return the data right away. The way you should do it is to specify a callback function which will handle the response data.

问题是Ajax请求是异步工作的。所以你不能马上返回数据。您应该这样做的方法是指定一个回调函数来处理响应数据。

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;
}​