在呈现 ajax 响应后执行 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12562595/
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-08-23 10:42:52  来源:igfitidea点击:

Execute Javascript after ajax response has been rendered

javascriptjquery

提问by amit_saxena

I want to execute a piece of javascript after the ajax response has been rendered. The javascript function is being generated dynamically during the ajax request, and is in the ajax response. 'complete' and 'success' events to not do the job. I inspected the ajax request in Firebug console and response hasn't been rendered when the complete callback executes.

我想在呈现 ajax 响应后执行一段 javascript。javascript 函数是在ajax 请求期间动态生成的,并且在ajax 响应中。'complete' 和 'success' 事件不能完成这项工作。我在 Firebug 控制台中检查了 ajax 请求,并且在执行完整回调时没有呈现响应。

Does not work:

      function reloadForm() {
        jQuery.ajax({
          url: "<generate_form_url>",
          type: "GET",
          complete: custom_function_with_js_in_response()
        });
      };

ajaxComplete does the job, but it executes for all the ajax calls on the page. I want to avoid that. Is there a possible solution?

ajaxComplete 完成这项工作,但它会为页面上的所有 ajax 调用执行。我想避免这种情况。有可能的解决方案吗?

$('#link_form').ajaxComplete(function() {
  custom_function_with_js_in_response();
});

回答by Bruno Guerra

you can also use $.ajax(..).done( do_things_here() );

你也可以使用 $.ajax(..).done( do_things_here() );

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>"
    }).done(function() {
      do_something_here();
    });
});

});

});

or is there another way

或者还有其他方法

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>",
        success: function(data){
          do_something_with(data);
        }
    })
});

});

});

Please, utilize this engine for share your problem and try solutions. Its very efficient.

请利用此引擎分享您的问题并尝试解决方案。它非常有效。

http://jsfiddle.net/qTDAv/7/(PS: this contains a sample to try)

http://jsfiddle.net/qTDAv/7/(PS:这包含一个可以尝试的示例)

Hope to help

希望有所帮助

回答by techfoobar

Checking (and deferring call if needed) and executing the existence of the callback function might work:

检查(并在需要时推迟调用)并执行回调函数的存在可能会起作用:

// undefine the function before the AJAX call
// replace myFunc with the name of the function to be executed on complete()
myFunc = null; 

$.ajax({
    ...
    complete: function() {
       runCompleteCallback(myFunc);
    },
    ...
});

function runCompleteCallback(_func) {
    if(typeof _func == 'function') {
        return _func();
    }
    setTimeout(function() {
        runCompleteCallback(_func);
    }, 100);
}

回答by Kapil Sharma

Can't help a lot without code. As an general example from JQuery ajax complete page

没有代码就无济于事。作为JQuery ajax 完整页面的一般示例

$('.log').ajaxComplete(function(e, xhr, settings) {
    if (settings.url == 'ajax/test.html') {
        $(this).text('Triggered ajaxComplete handler. The result is ' +
                 xhr.responseHTML);
    }
});

In ajaxComplete, you can put decisions to filter the URL for which you want to write code.

在 ajaxComplete 中,您可以决定过滤要为其编写代码的 URL。

回答by STO

Try to specify function name without ()in ajax options:

尝试在没有()ajax 选项的情况下指定函数名称:

function reloadForm() {
        jQuery.ajax({
          url: "<generate_form_url>",
          type: "GET",
          complete: custom_function_with_js_in_response
        });
      };