Javascript - 无法从嵌套函数访问本地范围对象

发布于 2024-10-16 02:06:32 字数 945 浏览 2 评论 0原文

我试图让一个函数从另一个页面上的 php 文件中获取一个对象。我正在使用 jQuery ajax 函数来执行 json 抓取,它工作正常。问题是当我尝试从函数返回该对象时。

我第一次记录该对象(在 success 函数内)时,它在控制台中是正确的,但函数 getGantt() 返回的对象记录为“未定义”。

如何从函数中取出这个对象?

我的代码:

    function getGantt(requestNumber){
        var ganttObject;
        $.ajax({
               type: "POST",
               url: "get_gantt.php",
               data: {request_number: requestNumber},
               success: function(returnValue){
                     ganttObject = $.parseJSON(returnValue);
                    console.log(ganttObject); //this logs a correct object in the console

                }
        });
        return ganttObject;
    }

    $(function(){ //document ready function

        var requestNumber = $('#request_number').text();

        var ganttObject = getGantt(requestNumber);
        console.log(ganttObject); //this logs "undefined"

    }); //end document ready function

I am trying to have a function grab an object from a php file on another page. I'm using the jQuery ajax function to to do the json grab, which is working correctly. The issue is when I try to return that object from the function.

The first time I log the object (from within the success function) it is correct in the console, but the returned object from the function getGantt() logs as "undefined".

How do I get this object out of the function?

My code:

    function getGantt(requestNumber){
        var ganttObject;
        $.ajax({
               type: "POST",
               url: "get_gantt.php",
               data: {request_number: requestNumber},
               success: function(returnValue){
                     ganttObject = $.parseJSON(returnValue);
                    console.log(ganttObject); //this logs a correct object in the console

                }
        });
        return ganttObject;
    }

    $(function(){ //document ready function

        var requestNumber = $('#request_number').text();

        var ganttObject = getGantt(requestNumber);
        console.log(ganttObject); //this logs "undefined"

    }); //end document ready function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹苦笑 2024-10-23 02:06:32

Ajax 中的 A 是缩写词的重要组成部分。异步 JavaScript 和 XML 是异步的。

$.ajax({success:someFunction}) 表示发出 HTTP 请求,当响应到达时,运行 someFunction

return ganttObject< /code> 在响应到达之前运行。

您应该对内部 someFunction 中的数据执行任何您想要执行的操作,而不是尝试将数据返回到调用函数。

The A in Ajax is an important part of the acronym. Asynchronous JavaScript and XML is asynchronous.

$.ajax({success:someFunction}) means Make an HTTP request and when the response arrives, run someFunction

return ganttObject runs before the response arrives.

You should do anything you want to do with the data inside someFunction and not try to return data to the calling function.

当梦初醒 2024-10-23 02:06:32

AJAX 中的 A 代表异步。因此,调用立即返回,并且一旦完成,就会调用成功回调。

因此,只需更改代码以使用回调即可:

function getGantt(requestNumber, callback) {
    var ganttObject;
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "get_gantt.php",
        data: {request_number: requestNumber},
        success: function(returnValue){
            callback(returnValue);
        }
    });
}

$(function() {

    var requestNumber = $('#request_number').text();

    var ganttObject = getGantt(requestNumber, function(ganttObject) {
        console.log(ganttObject);
    });

});

顺便说一句,我还删除了这个 parseJSON 内容 - 将 dataType 设置为 json 即可完成工作,并且不那么脏。

The A in AJAX stands for asynchronous. So the call immediately returns and as soon as it finishes, the success callback is called.

So, simply change your code to use a callback:

function getGantt(requestNumber, callback) {
    var ganttObject;
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "get_gantt.php",
        data: {request_number: requestNumber},
        success: function(returnValue){
            callback(returnValue);
        }
    });
}

$(function() {

    var requestNumber = $('#request_number').text();

    var ganttObject = getGantt(requestNumber, function(ganttObject) {
        console.log(ganttObject);
    });

});

Btw, I've also removed this parseJSON stuff - setting dataType to json does the job and is less dirty.

心是晴朗的。 2024-10-23 02:06:32

我至少知道为什么它不归还。 ganttObject 可能位于相同的范围内,但 success 函数最终在 XMLHTTP 对象的 readyState 回调中运行,因此它与 getGantt 函数位于不同的线程上。您可以将 $(function(){... 代码与您的成功函数分开吗?

I know why it's not returning it at least. The ganttObject may be in the same scope, but the success function is ultimately running in the readyState callback from the XMLHTTP object, so it's on a different thread than the getGantt function. Can you make the $(function(){... code apart of your success function?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文