0

I'm having a fuzzy brain day or something...I want to set a var = data returned from an ajax call and use it as a parameter in a morris chart.

 function GetICCGraphData() {
        return $.ajax({
            url: "/url/"
        });
    }

 $(function() {
        var promise = GetICCGraphData();

        Morris.Area({
                element: 'icc-graph',
                data: promise.success(function (data) { return data; }),
...<snip>...

the url returns JSON and if I hit the url manually and paste the data into data: <paste> the chart works as it should...intially I tried to do it like one might think it should be...to just use the function as the value for data...then I got off on this 'promise' thing....so I just need to figure out how to make a variable equal data from an ajax call. Seems like it should be easy so I dunno, maybe I've just been staring at it for too long?

thanks people

J Benjamin
  • 4,722
  • 6
  • 29
  • 39
  • Seems like what you have should work, it just wouldn't happen right away because the $.ajax is asynchronous, you could run your ajax synchronously though. – Shriike Oct 13 '14 at 19:52
  • @Shriike I am getting a console error " Cannot read property 'x' of undefined " in the morris.js ... fiddler shows the ajax call hitting successfully and returning the proper data. Re: making it synchronous...just need an async=false to do that, ya? – J Benjamin Oct 13 '14 at 19:56

2 Answers2

2

try

function GetICCGraphData() {
        return $.ajax({
            url: "/url/"
        });
    }

 $(function() {
        var promise = GetICCGraphData();
        promise.success(function (data) { 
            Morris.Area({
                element: 'icc-graph',
                data: data,
            })

        })
})

...<snip>...
1

Try instead

$.get( "/url/", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

From: http://api.jquery.com/jquery.get/

Integrated into your code:

$(function() {

    $.get("/url/", function(data) {
        Morris.Area({
                    element: 'icc-graph',
                    data: data,
    ...<snip>...
Brian
  • 172
  • 1
  • 6
  • awesome..thanks for posting the follow up integration...this works great and I think I understand it so I'm good to go. thanks for the response – J Benjamin Oct 13 '14 at 20:02