如何从函数本身之外的ajax调用访问数据

时间:2015-06-04 17:56:24

标签: javascript jquery json jsonp

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

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

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

1 个答案:

答案 0 :(得分:2)

你可以做这样的事情!

 _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);
相关问题