将json传递给jquery图(flot)的问题

时间:2010-04-27 01:51:23

标签: jquery json flot

我试图检索一些json以传入flot图。我知道json是正确的,因为我对它进行了硬编码检查,但我很确定我没有通过,因为它没有出现。 这是javascript:

var total = $.ajax({
  type: "POST",
  async: false,
  url: "../api/?key=xxx&api=report&crud=return_months&format=json"
}).responseText;
//var total = $.evalJSON(total);
var plot = $.plot($("#placeholder"),total);

这是json:

[ { data: [[1,12], [2,43], [3,10], [4,17], ], label: "E-File"}, { data: [[1,25], [2,35], [3,3], [4,5], ], label: "Bank Products" }, { data: [[1,41], [2,87], [3,30], [4,29], ], label: "All Returns" } ], {series: {lines: { show: true },points: { show: true }}, grid: { hoverable: true, clickable: true }, yaxis: { min: 0, max: 100 }, xaxis: { ticks: [[1,"January"],[2,"February"],[3,"March"],[4,"April"],[5,"May"],[6,"June"],[7,"July"],[8,"August"],[9,"September"],[10,"October"],[11,"November"],[12,"December"]] }}

1 个答案:

答案 0 :(得分:1)

确保同时设置dataType: "json"选项。顺便说一句,您可以在success回调函数中执行此操作,而无需在等待响应时锁定UI,如下所示:

$.ajax({
  type: "POST",
  dataType: "json", 
  url: "../api/?key=xxx&api=report&crud=return_months&format=json",
  success: function(total) {
    var plot = $.plot($("#placeholder"),total);
    //do more work if needed
  }
});

或者,使用$.post()以更短的形式执行相同的操作,如下所示:

$.post("../api/?key=xxx&api=report&crud=return_months&format=json", 
  function(total) {
    var plot = $.plot($("#placeholder"),total);
    //do work
  }, "json");