Rich Strle
Rich Strle

Reputation: 51

Can see the json coming back in Developer tools but it's not getting to my $.ajax function

I've got a jqplot bar chart and I've attached an onclick event to the bars. When I call the ajax function it all seems to work but I'm not getting any data back. The alert('Success') is firing but ret is still null. I can watch this in Developer Tools and see the json coming back but it's not getting into ret. What am I doing wrong?

    $('#chartMonthly').bind('jqplotDataClick', 
            function (ev, seriesIndex, pointIndex, data) {
    $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
    var ret = null;
    var url = 'wmHours.php';
    var obj = {};
    obj.m = "1";
    obj.d = "4";
    obj.y = '2013';
    $.ajax({
        async: false,
        url: url,
        type: 'post',
        data: obj,
        dataType:"json",
        success: gotData(ret)
        });  // End ajax

    }); // End function (ev, seriesIndex, pointIndex, data), end bind

function gotData(ret){

    alert('Success!');

}

Upvotes: 0

Views: 99

Answers (2)

charlietfl
charlietfl

Reputation: 171690

Change success: gotData(ret) to success: gotData.

You only want to provide the callback handler name unless you wrap the call in an anonymous function

Upvotes: 1

A. Wolff
A. Wolff

Reputation: 74420

Try this:

$('#chartMonthly').bind('jqplotDataClick', 
            function (ev, seriesIndex, pointIndex, data) {
    $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
    //var ret = null;  remove this line as it is useless here
    var url = 'wmHours.php';
    var obj = {};
    obj.m = "1";
    obj.d = "4";
    obj.y = '2013';
    $.ajax({
        //async: false,  you shouldn't do this!!!
        url: url,
        type: 'post',
        data: obj,
        dataType:"json",
        success: function(ret){gotData(ret);}
        });  // End ajax

    }); // End function (ev, seriesIndex, pointIndex, data), end bind

function gotData(ret){

    alert('Success :'+ret);

}

BTW setting async: false, to any ajax request is really a bad idea!

Upvotes: 1

Related Questions