我试图从Javascript返回Array但它总是返回undefined。 我的示例代码是
$(function () {
var x = fetchData();
alert(x);
})
function fetchData() {
var chartdata = [];
$.ajax({
url: '/home/FetchLiveData',
data: {},
type: 'GET',
success: function (data) {
ObjData = $.parseJSON(data);
$.each(ObjData, function (i) {
var TagName = ObjData[i].DisplayTagName;
var TagValue = ObjData[i].TagValue;
var dateString = ObjData[i].ValueDateTime.substr(6);
var currentTime = new Date(parseInt(dateString));
var month = ('0' + (currentTime.getMonth() + 1)).slice(-2)
var day = ('0' + (currentTime.getDate() + 1)).slice(-2)
var year = currentTime.getFullYear();
var hours = ('0' + (currentTime.getHours() + 1)).slice(-2)
var min = ('0' + (currentTime.getMinutes() + 1)).slice(-2)
var sec = ('0' + (currentTime.getSeconds() + 1)).slice(-2)
var date = year + "-" + month + "-" + day + " " + hours + ":" + min + ":" + sec;
var t = {};
t[TagName] = TagValue;
t['date'] = date;
chartdata.push(t);
});
}
});
return chartdata;
}
我的示例JSON字符串是
[{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":98.1050300000,"ValueDateTime":"\/Date(1485607746120)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":98.1050300000,"ValueDateTime":"\/Date(1485607751113)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":99.0123000000,"ValueDateTime":"\/Date(1485607756153)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":99.0123000000,"ValueDateTime":"\/Date(1485607761120)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":99.0123000000,"ValueDateTime":"\/Date(1485607766087)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":99.0369000000,"ValueDateTime":"\/Date(1485607771127)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":68.0764200000,"ValueDateTime":"\/Date(1485607806337)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":99.1101300000,"ValueDateTime":"\/Date(1485607776177)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":50.9594700000,"ValueDateTime":"\/Date(1485607781137)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":60.7912600000,"ValueDateTime":"\/Date(1485607786167)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":65.1429600000,"ValueDateTime":"\/Date(1485607791210)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":65.1429600000,"ValueDateTime":"\/Date(1485607796080)\/"},{"DisplayTagName":"Temperature","DisplayTagColor":"#000000","TagValue":65.1429600000,"ValueDateTime":"\/Date(1485607801190)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475134.1000000000,"ValueDateTime":"\/Date(1485607746303)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475186.2000000000,"ValueDateTime":"\/Date(1485607751293)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475214.3000000000,"ValueDateTime":"\/Date(1485607756333)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475243.7000000000,"ValueDateTime":"\/Date(1485607761300)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475283.7000000000,"ValueDateTime":"\/Date(1485607766267)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475358.0000000000,"ValueDateTime":"\/Date(1485607771307)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475382.0000000000,"ValueDateTime":"\/Date(1485607776357)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475382.0000000000,"ValueDateTime":"\/Date(1485607781320)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475394.8000000000,"ValueDateTime":"\/Date(1485607786347)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475417.9000000000,"ValueDateTime":"\/Date(1485607791300)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475468.9000000000,"ValueDateTime":"\/Date(1485607796350)\/"},{"DisplayTagName":"Density","DisplayTagColor":"#ed4423","TagValue":475506.7000000000,"ValueDateTime":"\/Date(1485607801407)\/"}]
我在这里失踪了什么?有什么帮助吗?
感谢。