来自 $.getJSON 和节点异步的 returns 不正确 - 服务器端 node/express jsonp
Incorrect returns from $.getJSON and node-async - server side node/express jsonp
我在使用 $.getJSON 和节点异步到服务器 运行 node/express 和 jsonp 响应时遇到一个奇怪的错误。
我使用节点异步遍历数组对服务器进行了两次调用,但是在完成所有操作后,第二个结果具有一些属性,其中包含第一个的值。
奇怪的是服务器端响应正在返回 - 因为返回的属性之一是正确的 - 但另一个 属性 看起来它在第二次调用时被 属性从第一个开始。
在服务器端,我调试了它并检查了返回正确结果的 res.jsonp 行,我还用一些控制台日志记录服务器端检查了它。
下面的代码 - TLDR 结果如下:
//Expecting results to be:
[
{"series":1, options:{serverSays:"Here is series 1"},data: [a:1,b:2,c:3]},
{"series":2, options:{serverSays:"Here is series 2"}, data: [x:9, y:8, z:7]}
]
//Actual Results are
[
{"series":1, options:{serverSays:"Here is series 1"}, data: [a:1,b:2,c:3]},
{"series":2, options:{serverSays:"Here is series 2"}, data: [a:1,b:2,c:3]}
]
这是代码的简化版本(删除了很多垃圾但逻辑保持不变):
var series = [
{id: 1},
{id: 2}
]
var results = [];
//A function to get data from the server and return it
function getData(id, cb)
{
var url = serverUrl + "/GetSomeData/" + id;
$.getJSON(url)
.done(function (serverResponse)
{
console.log("Response from server: ", serverResponse)
cb({"series": id, options: serverResponse.options, data: serverResponse.data});
}
}
//A function to iterate through the series using async.forEach
function iterator(s, callback)
{
getData(s.id, function(data)
{
//Use the data
results.push(data);
//Tell async we are done with this iteration
callback()
});
}
function done(err)
{
console.log("Finished");
//Expecting results to be
//[
// {"series":1, options:{serverSays:"You Requested Series 1"},data: [a:1,b:2,c:3]},
// {"series":2, options:{serverSays:"You Requested Series 2"}, data: [x:9, y:8, z:7]}
//]
//Actual Results are
//[
// {"series":1, options:{serverSays:"You Requested Series 1"}, data: [a:1,b:2,c:3]},
// {"series":2, options:{serverSays:"You Requested Series 2"}, data: [a:1,b:2,c:3]}
//]
}
async.eachSeries(series, iterator, done);
我已经尝试使用异步系列、异步并行以及承诺样式 getJSON 和回调样式 getJSON - 都显示相同的错误。
可能是我的客户端代码,或者我在使用服务器端 jsonp 做一些愚蠢的事情。
服务器端,我构建数据然后去
return res.jsonp(output);
有什么想法吗?
我不得不承认,我找不到您的代码有任何问题。为了穷尽所有选项,也许您可以尝试 async.map?
var series = [
{id: 1},
{id: 2}
]
//A function to get data from the server and return it
function getData(id, cb)
{
var url = serverUrl + "/GetSomeData/" + id;
$.getJSON(url)
.done(function (serverResponse)
{
console.log("Response from server: ", serverResponse);
cb({"series": id, options: serverResponse.options, data: serverResponse.data});
});
}
//A function to iterate through the series using async.forEach
function iterator(s, callback)
{
getData(s.id, function(data)
{
//Tell async to add data to the result list
// and continue with the next iteration.
callback(null, data); // err = null
});
}
function done(err, result)
{
console.log("Finished");
}
// Note that map doesn't perform the iterator in order, but the result list is in order.
async.map(series, iterator, done);
您是否尝试检查开发人员工具的网络选项卡以查看浏览器收到的来自服务器的响应?
如果响应与预期输出相同,则此处的图片可能缺少某些内容。
呵呵。看起来我被一个外部函数调用发现了,它严重破坏了 chrome 检查器!!
我终于意识到 chrome 控制台没有在检查器中显示有效数据,但是如果我 JSON.stringify 对象显示正确。
例如
console.log("Server response is: ", serverResponse) //In the getJSON callback
将为不正确的对象提供树视图,如下所示:
{"series":2, options:{serverSays:"Here is series 2"}, data: [a:1,b:2,c:3]} //Incorrect data property
但是在做
console.log("Server response is: ", JSON.stringify(serverResponse))
显示正确 JSON:
{"series":2, options:{serverSays:"Here is series 2"}, data: [x:9, y:8, z:7]} //Correct data property
--
原因?我对数据做了什么。我从示例中删除的 "junk taken out" 行之一是调用 highcharts 图表来设置系列数据 - 沿着
行
getData(s.id, function(data)
{
//THE OMITTED LINE
chart.series[someSeries].setData(data.data);
//Tell async to add data to the result list
// and continue with the next iteration.
callback(null, data); // err = null
});
这是我在图表定义中遇到的一个错误,但它并没有优雅地失败,而是以某种方式破坏了 chrome 检查器...!
我猜节点异步 + $.getJSON + 多个函数 + 回调的范围地狱,这是最后一根稻草。去图吧!
我在使用 $.getJSON 和节点异步到服务器 运行 node/express 和 jsonp 响应时遇到一个奇怪的错误。
我使用节点异步遍历数组对服务器进行了两次调用,但是在完成所有操作后,第二个结果具有一些属性,其中包含第一个的值。
奇怪的是服务器端响应正在返回 - 因为返回的属性之一是正确的 - 但另一个 属性 看起来它在第二次调用时被 属性从第一个开始。
在服务器端,我调试了它并检查了返回正确结果的 res.jsonp 行,我还用一些控制台日志记录服务器端检查了它。
下面的代码 - TLDR 结果如下:
//Expecting results to be:
[
{"series":1, options:{serverSays:"Here is series 1"},data: [a:1,b:2,c:3]},
{"series":2, options:{serverSays:"Here is series 2"}, data: [x:9, y:8, z:7]}
]
//Actual Results are
[
{"series":1, options:{serverSays:"Here is series 1"}, data: [a:1,b:2,c:3]},
{"series":2, options:{serverSays:"Here is series 2"}, data: [a:1,b:2,c:3]}
]
这是代码的简化版本(删除了很多垃圾但逻辑保持不变):
var series = [
{id: 1},
{id: 2}
]
var results = [];
//A function to get data from the server and return it
function getData(id, cb)
{
var url = serverUrl + "/GetSomeData/" + id;
$.getJSON(url)
.done(function (serverResponse)
{
console.log("Response from server: ", serverResponse)
cb({"series": id, options: serverResponse.options, data: serverResponse.data});
}
}
//A function to iterate through the series using async.forEach
function iterator(s, callback)
{
getData(s.id, function(data)
{
//Use the data
results.push(data);
//Tell async we are done with this iteration
callback()
});
}
function done(err)
{
console.log("Finished");
//Expecting results to be
//[
// {"series":1, options:{serverSays:"You Requested Series 1"},data: [a:1,b:2,c:3]},
// {"series":2, options:{serverSays:"You Requested Series 2"}, data: [x:9, y:8, z:7]}
//]
//Actual Results are
//[
// {"series":1, options:{serverSays:"You Requested Series 1"}, data: [a:1,b:2,c:3]},
// {"series":2, options:{serverSays:"You Requested Series 2"}, data: [a:1,b:2,c:3]}
//]
}
async.eachSeries(series, iterator, done);
我已经尝试使用异步系列、异步并行以及承诺样式 getJSON 和回调样式 getJSON - 都显示相同的错误。
可能是我的客户端代码,或者我在使用服务器端 jsonp 做一些愚蠢的事情。
服务器端,我构建数据然后去
return res.jsonp(output);
有什么想法吗?
我不得不承认,我找不到您的代码有任何问题。为了穷尽所有选项,也许您可以尝试 async.map?
var series = [
{id: 1},
{id: 2}
]
//A function to get data from the server and return it
function getData(id, cb)
{
var url = serverUrl + "/GetSomeData/" + id;
$.getJSON(url)
.done(function (serverResponse)
{
console.log("Response from server: ", serverResponse);
cb({"series": id, options: serverResponse.options, data: serverResponse.data});
});
}
//A function to iterate through the series using async.forEach
function iterator(s, callback)
{
getData(s.id, function(data)
{
//Tell async to add data to the result list
// and continue with the next iteration.
callback(null, data); // err = null
});
}
function done(err, result)
{
console.log("Finished");
}
// Note that map doesn't perform the iterator in order, but the result list is in order.
async.map(series, iterator, done);
您是否尝试检查开发人员工具的网络选项卡以查看浏览器收到的来自服务器的响应?
如果响应与预期输出相同,则此处的图片可能缺少某些内容。
呵呵。看起来我被一个外部函数调用发现了,它严重破坏了 chrome 检查器!!
我终于意识到 chrome 控制台没有在检查器中显示有效数据,但是如果我 JSON.stringify 对象显示正确。
例如
console.log("Server response is: ", serverResponse) //In the getJSON callback
将为不正确的对象提供树视图,如下所示:
{"series":2, options:{serverSays:"Here is series 2"}, data: [a:1,b:2,c:3]} //Incorrect data property
但是在做
console.log("Server response is: ", JSON.stringify(serverResponse))
显示正确 JSON:
{"series":2, options:{serverSays:"Here is series 2"}, data: [x:9, y:8, z:7]} //Correct data property
--
原因?我对数据做了什么。我从示例中删除的 "junk taken out" 行之一是调用 highcharts 图表来设置系列数据 - 沿着
行getData(s.id, function(data)
{
//THE OMITTED LINE
chart.series[someSeries].setData(data.data);
//Tell async to add data to the result list
// and continue with the next iteration.
callback(null, data); // err = null
});
这是我在图表定义中遇到的一个错误,但它并没有优雅地失败,而是以某种方式破坏了 chrome 检查器...!
我猜节点异步 + $.getJSON + 多个函数 + 回调的范围地狱,这是最后一根稻草。去图吧!