javascript - 如何向 d3.xhr 调用添加全局监听器

标签 javascript ajax d3.js

我们是否可以为 d3.xhr 调用添加任何全局监听器,就像我们在 ExtJS 中那样。 Ext.Ajax.on("beforerequest", function(conn, options, eOpts){)};

我尝试使用 https://github.com/mbostock/d3/wiki/Requests#on 中指定的“on”

在尝试的过程中

    d3.xhr.on("beforesend", function(request){
        console.log("inside on");
    });

我收到以下错误。 未捕获的类型错误:d3.xhr.on 不是函数

尝试下面的代码时

    d3.xhr(url, callback).on("beforesend", function(request){
        console.log("inside on");
    });

抛出以下错误。 未捕获的类型错误:无法读取未定义的属性“on”

我需要做一些特殊的事情来启用听众吗?

提前致谢。

最佳答案

您不需要执行任何特殊操作,但必须使用带有 .get() 的长格式才能使用 .on() 处理程序。也就是说,您不能提供回调函数作为 d3.xhr() 的第二个参数:

d3.xhr("http://www.example.com") // note no callback function!
.on("beforesend", function(request){
    console.log("inside on");
})
.get(function(error, data) {
    console.log(error, data);
});

完整演示 here 。另请参阅this example .

关于javascript - 如何向 d3.xhr 调用添加全局监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722823/

相关文章:

javascript - Angular `watch` 未按预期运行

javascript - 在 setInterval 中获取正确的上下文

javascript - 使用 AJAX 在 PHP 文件中调用 PHP 方法

svg - 如何在 d3.js 中的图表节点上添加形状?

javascript - D3 动画导致 Chrome 上的 CPU 使用率过高/崩溃

javascript - 删除初始化时的 d3 轴过渡

javascript - 排序在 jQuery 数据表中不起作用

javascript - 光标 :pointer not working on input type file

c# - ASP.NET 中带有 UpdatePanel 的 CodeMirror

jquery - ajax 调用 Controller 时出现错误 500 - MVC Razor