Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 执行异步操作后执行函数_Javascript_Jquery_Asynchronous - Fatal编程技术网

Javascript 执行异步操作后执行函数

Javascript 执行异步操作后执行函数,javascript,jquery,asynchronous,Javascript,Jquery,Asynchronous,我遇到了一些问题,因为我试图在应用程序的其他部分重用代码(可以使用) 现在,我有一个共同点: $.ajax({ url: JSONurl, dataType: "json", cache: false, data: params, success: function(data){ // do stuff after the result is retrieved } }); 事情进展顺利,但我想做些类似的事情: function ultrameg

我遇到了一些问题,因为我试图在应用程序的其他部分重用代码(可以使用)

现在,我有一个共同点:

$.ajax({
   url: JSONurl,
   dataType: "json",
   cache: false,
   data: params,
   success: function(data){
      // do stuff after the result is retrieved
   }
});
事情进展顺利,但我想做些类似的事情:

function ultramegafunction (){
    var ajax_data = asyncAjaxFunction();
    // When ajax_data is filled do other stuff
}
这样,我就可以在其他需要的函数中使用
asyncAjaxFunction()
,而无需重写所有代码,也无需在成功部分添加特定内容


有人知道我是如何做到这一点的吗?

使用jQuerys
Deferred
对象,这些对象隐式地混合到jXHR对象中。因此,您需要
返回
jXHR对象,然后在其他位置绑定处理程序

function asyncAjaxFunction() {
    return $.ajax({
        url: JSONurl,
        dataType: "json",
        cache: false,
        data: params,
        success: function(data){
           // do stuff after the result is retrieved
        }
    });
}
还有别的地方

function ultramegafunction (){
    asyncAjaxFunction().done(function( ajaxData ) {
        // When ajax_data is filled do other stuff
    });
}

参考:

使用jQuerys
延迟的
对象,这些对象隐式地混合到jXHR对象中。因此,您需要
返回
jXHR对象,然后在其他位置绑定处理程序

function asyncAjaxFunction() {
    return $.ajax({
        url: JSONurl,
        dataType: "json",
        cache: false,
        data: params,
        success: function(data){
           // do stuff after the result is retrieved
        }
    });
}
还有别的地方

function ultramegafunction (){
    asyncAjaxFunction().done(function( ajaxData ) {
        // When ajax_data is filled do other stuff
    });
}
参考资料:

试试这个

"asyncAjaxFunction" = function(options){
    var xhr = $.ajax( {
        'url' : options.url,
        'dataType' : (options.dataType) ? options.dataType : 'JSON',
        'type' : (options.type) ? options.type : 'GET',
        'async' : (options.async) ? options.async : false,
        'data' : (options.data) ? options.data : '',
        'cache' : (options.cache) ? options.cache : false,
        'success' : (options.success)? (options.success) :function(data) {
        },
        'error' : (options.error)? (options.error) :function() {
        }
    });
    return (xhr.status === 200) ? xhr.responseText : '';

}
现在,您可以使用适当的参数从任何位置调用asyncAjaxFunction()

"asyncAjaxFunction" = function(options){
    var xhr = $.ajax( {
        'url' : options.url,
        'dataType' : (options.dataType) ? options.dataType : 'JSON',
        'type' : (options.type) ? options.type : 'GET',
        'async' : (options.async) ? options.async : false,
        'data' : (options.data) ? options.data : '',
        'cache' : (options.cache) ? options.cache : false,
        'success' : (options.success)? (options.success) :function(data) {
        },
        'error' : (options.error)? (options.error) :function() {
        }
    });
    return (xhr.status === 200) ? xhr.responseText : '';

}

现在,您可以使用适当的参数从任何位置调用asyncAjaxFunction()。

数据存储在哪里?@AntonioLaguna:不确定问题是什么,但ajax请求的结果也会传递到
done()
处理程序中。在上面的代码片段中,我给它起了一个名字
ajaxData
。我不知道那件事!非常感谢@Jandyan,数据存储在哪里?@AntonioLaguna:不确定问题是什么,但ajax请求的结果也会传递到
done()
处理程序中。在上面的代码片段中,我给它起了一个名字
ajaxData
。我不知道那件事!非常感谢你,詹迪