我们是否可以为 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/