0
             function getDept() {
                var deplist;
                //Get Department
                $.ajax({
                    type: 'GET',
                    url: '@(Url.Action("getDeptFK", "Settings"))',
                    dataType: "jsonp",
                    success: function (data) {
                        deplist = JSON.parse(JSON.stringify(data));
                    }
                });
                return deplist;
            }

function return undefined but debug value in success work !

Thanawat.s
  • 15
  • 3
  • 1
    I hope you realize that `JSON.parse(JSON.stringify(data))` is quit unnecessary. It's as useful as `x - 5 + 5`. Just use `data` directly. There is no reason to convert the object/array to JSON first and then parse the JSON back to an object/array. – Felix Kling Feb 20 '14 at 03:37

1 Answers1

2

Ajax is asynchronous so you can't return anything from it.

One way is to rewrite your getDept() function to this:

function getDept() {
    var deplist;
    //Get Department

    return $.ajax({
        type: 'GET',
        url: '@(Url.Action("getDeptFK", "Settings"))',
        dataType: "jsonp",
        success: function (data) {
            deplist = JSON.parse(JSON.stringify(data));
        }
     });
}

then you can pass it to a variable:

var result = getDept();

and get the data that is returned by the AJAX call like this:

result.success(function (data) {
    console.log(data);
});
Felix
  • 37,892
  • 8
  • 43
  • 55