0

我试图检索一些 json 以传递到一个浮点图。我知道 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"]] }}
4

1 回答 1

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");
于 2010-04-27T01:53:18.720 回答