在呈现ajax响应后执行Javascript

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

Does not work: function reloadForm() { jQuery.ajax({ url: "", type: "GET", complete: custom_function_with_js_in_response() }); }; 

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

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

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

 $(document).ready(function() { $('#obj').click(function() { $.ajax({ url: "" }).done(function() { do_something_here(); }); }); 

});

还是有另一种方式

 $(document).ready(function() { $('#obj').click(function() { $.ajax({ url: "", success: function(data){ do_something_with(data); } }) }); 

});

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

http://jsfiddle.net/qTDAv/7/(PS :这包含要尝试的样本)

希望能有所帮助

检查(并在需要时延迟调用)并执行回调函数的存在可能有效:

 // 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); } 

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

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

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

尝试在ajax选项中指定不带()函数名称:

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