如何在$ .ajax()的成功函数中加载Javascript文件? [英] How do you load a Javascript file inside of the success function for $.ajax()?

查看:118
本文介绍了如何在$ .ajax()的成功函数中加载Javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery的$ .ajax()来获取一些数据。我想将这些数据包含在弹出框中。我正在使用Twitter Bootstrap popovers。



它不起作用;我相信问题是,在数据到达之前,弹出窗口的JS会被加载。



我该怎么做:

 < script src ={{STATIC_URL}} js / bootstrap-popover.js>< / script>我的$ .ajax()成功函数里面有

  var request = $ .ajax({
url:requestUrl,
dataType:jsonp,
success:function(data) {
...
}


解决方案

在尝试为您提供更好的解决方案时,您的暂定结论对我来说没有意义。您应该能够在ajax调用之前很久就包含popover javascript,然后使用代码实际调用popover或在任何时候配置它成功处理程序中新添加的内容。



我没有理由在成功处理程序中加载popover js。事先加载它(以正常方式加载)你加载其他脚本)然后在你的ajax调用的成功处理程序中使用它。为了帮助你如何使用popover脚本,我们需要更多地了解你正在尝试用它做什么。 / p>

I am using jQuery's $.ajax() to get some data. I'd like to include that data in a popover dialog. I'm using the Twitter Bootstrap popovers.

It's not working; I believe the problem is that the JS for the popovers gets loaded before the data arrives.

How do I do something like:

<script src="{{ STATIC_URL }}js/bootstrap-popover.js"></script>

inside of my $.ajax() success function?

var request = $.ajax({
    url: requestUrl,
    dataType: "jsonp",
    success: function(data) {
       ...
    }

解决方案

In trying to offer you a better solution, your tentative conclusion doesn't make sense to me. You should be able to include the popover javascript long before your ajax call and then use code to actually invoke the popover or configure it on any newly added content in your success handler.

There is no reason I'm aware of to load the popover js inside the success handler. Load it beforehand (in the normal way you load your other scripts) and then use it in the success handler for your ajax call. To help with how you'd use the popover script, we'd need to know more about what you're trying to do with it.

这篇关于如何在$ .ajax()的成功函数中加载Javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆