0

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"]] }}

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

4 Comments

@Adam - Clarify a bit? Not working as in the success callback isn't running, it's erroring, other?
the success callback is running i just tested it out and put the json directly into a div
@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: jsonlint.com
ok let's go under the assumption that it's a javascript object instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.