我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 我正在尝试加载GeoJSON文件并使用它作为D3 v5的基础绘制一些图形.
问题是浏览器正在跳过d3.json()调用中包含的所有内容.我尝试插入断点进行测试,但浏览器会跳过它们,我无法弄清楚原因.
下面的代码片段.
d3.json("/trip_animate/tripData.geojson", function(data) {
console.log("It just works"); // This never logs to console.
//...all the rest
}
Run Code Online (Sandbox Code Playgroud)
代码从最初开始继续console.log(),但我省略了所有这些,因为我怀疑这个问题与d3.json调用本身有关.