javascript 在ajax函数外传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20228037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:03:03  来源:igfitidea点击:

Passing variable outside ajax function

javascriptjqueryajax

提问by MarkE

Please help - I have the following code which returns the variable 'results' in an alert. I want to use this variable outside of the function, and cannot get a callback, or outside declaration to work successfully. I'm sure I'm being a muppet, so I apologise for this basic question.....

请帮忙 - 我有以下代码,它在警报中返回变量“结果”。我想在函数之外使用这个变量,并且无法成功获得回调或外部声明。我确定我是一个布偶,所以我为这个基本问题道歉......

<script src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script>

$(function () {
    $.ajax({
        type: 'POST',
        dataType: 'jsonp',
        url: 'https://creator.zoho.com/api/json/my-company-culture/view/PageFeed_Report?scope=creatorapi&authtoken=d670dc68ac0f6d7ca389e7b206a25045',
        success: function (results) {
            var raw_result=JSON.stringify(results);
            alert(results);
        }
    });
});

 </script>

回答by Alnitak

The jQuery 1.5+ way of resolving this is by using deferred objects:

jQuery 1.5+ 解决这个问题的方法是使用延迟对象:

var res;

var ajax = $.ajax({
    type: 'POST',
    dataType: 'jsonp',
    url: ...
}).done(function(results) {
    // you may safely use results here
    res = results;
    ...
});

// you cannot safely use 'res' here, only in code
// invoked via a 'done' callback on the `$.ajax` promise

console.log(res);    // undefined

It would be incorrect to attempt to copy resultsinto some other variable in the outer scope unless you have ensured that no other code tries to access that variable until the AJAX call has completed.

results除非您确保在 AJAX 调用完成之前没有其他代码尝试访问该变量,否则尝试复制到外部作用域中的某个其他变量是不正确的。

回答by haldagan

Well, why not:

好吧,为什么不:

    <script src='http://code.jquery.com/jquery-1.9.1.js'></script>
    <script>
    function do_the_stuff(smth) {
         console.log(smth);//or do whatever you want to do with this data
    }
    $(function () {
        $.ajax({
            type: 'POST',
            dataType: 'jsonp',
            url: 'https://creator.zoho.com/api/json/my-company-culture/view/PageFeed_Report?scope=creatorapi&authtoken=d670dc68ac0f6d7ca389e7b206a25045',
            success: function (results) {
                var raw_result=JSON.stringify(results);
                do_the_stuff(results);
            }
        });
    });

    </script>

That works fine for me. I don't see any problem.

这对我来说很好用。我看不出有什么问题。

回答by Wayne

I'm not sure what good that would do but just save it to a globally defined variable.

我不确定这有什么好处,只是将它保存到一个全局定义的变量中。

var global;

$(function () {
    $.ajax({
        type: 'POST',
        dataType: 'jsonp',
        url: 'https://creator.zoho.com/api/json/my-company-culture/view/PageFeed_Report?scope=creatorapi&authtoken=d670dc68ac0f6d7ca389e7b206a25045',
        success: function (results) {
            var raw_result=JSON.stringify(results);
            global = results;
        }
    });
});

A better solution is to pass it to the function that you need it to go to.

更好的解决方案是将它传递给您需要它去的函数。

    success: function (results) {
        var raw_result=JSON.stringify(results);
        doMoreThings(results);
    }