tags:

views:

423

answers:

1

I trying to retrieve some json to pass into a flot graph. I know that json is right because I hard coded it to check, but I'm pretty sure that I'm not passing right because It's not showing up. Here's the 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);

here's the 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"]] }}
A: 

Make sure to set your dataType: "json" option as well. As an aside, you could do it in the success callback function as well without locking the UI while waiting for a response, like this:

$.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
  }
});

Alternatively, use $.post() to do the same in shorter form, like this:

$.post("../api/?key=xxx&api=report&crud=return_months&format=json", 
  function(total) {
    var plot = $.plot($("#placeholder"),total);
    //do work
  }, "json");
Nick Craver
ugh too many common mistakes.Thanks Nick :-)
Adam McMahon
actually I just tried both solutions and neither work
Adam McMahon
@Adam - Clarify a bit? Not working as in the `success` callback isn't running, it's erroring, other?
Nick Craver
the success callback is runningi just tested it out and put the json directly into a div
Adam McMahon
@Adam - What you're returning doesn't look like valid JSON, I'm not sure what structure you're after. Put the result in here to see what I mean: http://www.jsonlint.com/
Nick Craver
ok let's go under the assumption that it's a javascript object instead
Adam McMahon