【问题标题】:Ajax call Output into global variableAjax 调用输出到全局变量
【发布时间】:2015-08-30 17:30:39
【问题描述】:

如何将ajax调用的输出赋值给一个全局变量,使得输出可以在ajax调用之外使用?

var filterarray=new Array();
$.ajax({
            type: "GET",
            url: uri,
            dataType : "json",
            contentType : "application/json",           
            data: {
                input:filtervalue
            },  
            cache: false,
            success : function(response) {
                filterarray = response;
                console.log(response);

                });
            },
            error: function(error) { 
                console.log(error);
            }
            });
    }

【问题讨论】:

  • 代码在哪里?您面临的问题是什么?
  • 你的错误是什么?你在 console.log(response) 时得到一个输出?
  • 是的,我确实在 console.log 中出现错误。但是如果我在 ajax 调用之后将 console.log 用于 filterarray,它就不起作用

标签: javascript jquery ajax


【解决方案1】:

假设您有来自ajax 文档的这个简单示例:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

如您所见,在所有回调中添加 contextthis 引用:

所有回调中的 this 引用是上下文中的对象 在设置中传递给 $.ajax 的选项;如果没有指定上下文, 这是对 Ajax 设置本身的引用。

您也可以将变量附加到window

【讨论】:

    【解决方案2】:

    您可以将值分配给全局变量,就像代码中的其他任何地方一样。

    var value;
    
    $.ajax().done(function(responseValue) {
        value = responseValue;
    });
    

    虽然问题在于这段代码是异步的,如果你尝试在调用完成之前访问该值,例如在 $.ajax 块之后,该值将是未定义的。 jQuery $.ajax 调用返回 Promise 的实现,因此这是一个更好的做法:

    var ajaxPromise = $.ajax(...);
    // ...
    
    // here you want to access value form ajax call:
    ajaxPromise.done(function(value) {
        // do something with value
    });
    

    promise 的好处是你可以多次调用.done,它总是会用value 解决,即使它之前已经使用过。

    【讨论】:

    • Uncaught ReferenceError: ajaxPromise is not defined
    • 我不确定你做错了什么,但我已经用 jQuery 1.x 和 2.x 测试了这段代码,它可以工作。因此,我可以向您保证此代码有效,因为您已将 $.ajax 调用适应您的用例,而不仅仅是直接复制我的代码。
    【解决方案3】:

    试试这个:

    var result; 
    $.ajax({ 
           type: "POST", 
           async: false, 
           url: url, 
           data: postdata, 
           dataType: "json", 
           success: function (data) { 
           result= data; 
       } });
    result = jQuery.parseJSON(result);
    

    这里async:false用于将ajax返回值存储到全局result变量中。

    让我知道这些代码 sn-ps 是否有效。

    【讨论】:

    • 它为我工作。我可以看看你的代码吗?你能发布你的代码吗?
    【解决方案4】:

    Ajax 是异步的。所以代码执行后可能会得到响应。

    var filterarray=new Array();
    $.ajax({
                type: "GET",
                url: uri,
                dataType : "json",
                contentType : "application/json",           
                data: {
                    input:filtervalue
                },  
                cache: false,
                success : function(response) {
                    filterarray = response;
                    // do what ever you want to do with response here.
    
    
    
                    });
                },
                error: function(error) { 
                    console.log(error);
                }
                });
        }
    

    【讨论】:

      【解决方案5】:

      函数 getJson(url) { 返回 JSON.parse($.ajax({ 类型:'GET', 网址:网址, 数据类型:'json', 全局:假, 异步:假, 数据: { 过滤器:值 }, 成功:函数(数据){ 返回数据; } }).responseText); }

      以上代码解决了我的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        相关资源
        最近更新 更多