1
我試圖接收JSON字符串並將其饋送到canvasJS圖形中。我很難找出如何將我的字符串轉換爲正常工作。這裏就是我想,這是行不通的:嘗試從服務器解析並轉換JSON
function loadGraph(serialNum){
$.post("../Modules/LoadGraph_Mod/load_graph_mod.php",
{
submit: 'loadGraph',
serial:serialNum
},
function(data,status){
newDataPoints=data;
});
var chart = new CanvasJS.Chart("chartContainer",
{
interactivityEnabled: true,
zoomEnabled: true,
panEnabled: true,
backgroundColor:"#000",
axisX: { lineThickness: 1,
lineColor: "#333",
interval: 4
,
intervalType: "minutes",
valueFormatString: "h:mm",
labelFontColor: "#777"
},
axisY: { tickColor:"#333",
gridColor: "#222",
lineThickness: 1,
lineColor: "#333",
valueFormatString: "" ,
maximum: 100 },
data: [{
dataPoints : newDataPoints,
markerType: "none",
color: "#9c9a0c",
type: "area",
xValueType: "dateTime"
}]
});
chart.render();
}
我已經嘗試測試它,而是直接插入我的服務器響應:
newDataPoints='[{"x":1483648595798,"y":62},{"x":1483648598843,"y":63},{"x":1483648601969,"y":64},{"x":1483648605088,"y":65},{"x":1483648779011,"y":0},{"x":1483648847224,"y":16}]';
這給我的錯誤:未定義是不是目的。我試着運行JSON.parse,最後出現一個錯誤,表示無法識別的令牌'<'。
當我串直接粘貼到表像這樣它的工作原理:
data: [{
dataPoints : [{"x":1483648595798,"y":62},{"x":1483648598843,"y":63},{"x":1483648601969,"y":64},{"x":1483648605088,"y":65},{"x":1483648779011,"y":0},{"x":1483648847224,"y":16},{"x":1483648860414,"y":17},{"x":1483648862560,"y":18},{"x":1483648865650,"y":19},{"x":1483648868773,"y":20}],
markerType: "none",
color: "#9c9a0c",
type: "area",
xValueType: "dateTime"
}]
所以我想弄清楚如何將JSON字符串我從我的服務器回到它正常工作的轉換我的圖表。我會很感激任何幫助。謝謝。
當你正在數據從服務器返回,並試圖'JSON我會懷疑.parse',你實際上沒有找回JSON,而是一個網頁。 – powerc9000
'$ .post'是異步的。將函數移動到'function(data,status){'函數中。 –
[我如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –