【问题标题】:How to access the data from an ajax call outside the function itself如何从函数本身外部的 ajax 调用访问数据
【发布时间】:2015-06-04 17:56:24
【问题描述】:

此代码从另一个服务器获取一个 json 对象。

如何在函数之外访问响应?

(function($) {
    $.ajax({
        type: 'GET',
        url: url,
        async: false,
        jsonpCallback: 'callback',
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(json) {
            var data = json;
        }
    })
})(jQuery);

【问题讨论】:

  • 你能展示更多你的程序的结构吗?什么叫这个函数。您使用什么技术来回复 Ajax。
  • 我创建它是为了获取用于提供图表 (amchart) 的数据。
  • 看小提琴:jsfiddle.net/yvzSL/693

标签: javascript jquery json jsonp


【解决方案1】:

你可以这样做!

 _outerMethod: function(data) {
     //...do what you need to do 
 }, (function($) {
     $.ajax({
         type: 'GET',
         url: url,
         async: false,
         jsonpCallback: 'callback',
         contentType: "application/json",
         dataType: 'jsonp',
         success: function(json) {
             _outerMethod(json);
         }
     })
 })(jQuery);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 2011-08-15
    • 1970-01-01
    • 2011-07-04
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多