1

I have an ajax function that returns some data. What I need is, to use it another function as a parameter. Actually I did something like below , but it doesnt work. I need the return value of ajaxDataRenderer. How can I do that ?

    $(document).ready(function () {

        var ajaxDataRenderer = function (url) {
            var ret = null;
            $.ajax({
                async: false,
                url: url,
                dataType: "json",
                success: function (data) {
                    ret = data;
                }
            });
            return ret;
        };

        // The url for our json data
        var jsonurl = "Service/test.aspx";

        var plot2 = $.jqplot('chart2', ajaxDataRenderer(jsonurl));

    });

Thanks in advance,

Barış Velioğlu
  • 5,709
  • 15
  • 59
  • 105

3 Answers3

1

How about using real asynchronous AJAX:

var ajaxDataRenderer = function (url, successHandler) {
    $.ajax({
        url: url,
        dataType: "json",
        success: successHandler
   });
};

$(document).ready(function () {
    // The url for our json data
    var jsonurl = "Service/test.aspx";
    ajaxDataRenderer(jsonurl, function(data) {
        var plot2 = $.jqplot('chart2', data);
    });
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

You cannot do this since the ajax request is asynchronous. This question has been asked many times before: How to return value from $.getJSON

Community
  • 1
  • 1
Calum
  • 5,308
  • 1
  • 22
  • 27
0

You could call the 'jqplot' function inside the success callback -

success: function (data) {
    ret = data;
    var plot2 = $.jqplot('chart2', data);
}
ipr101
  • 24,096
  • 8
  • 59
  • 61