0

I'm working on a school project which uses a lot of ajax. To spare myself some time copy-pasting and a lot of room in the main code I want to create modular ajax functions. Found some examples on stackoverflow but somehow I'm not capable of getting the output that I want.

function retrieveAjax(url) {
    $.ajax({
        url: url, dataType: 'json',
        success: function(data) {
            console.log(data);

            return data;
        },
        error: function(request, status, error) {
            return [{request: request, status: status, error: error}];
        }
    });
}

console.log(retrieveAjax('../functions/getcourses.php'));

The log for the data appears, the request is made and results are shown, but in the last console it appears to return undefined.

Any help would be appreciated

Edit: After reading the original question on which my question was a duplicate, I tried this:

function foo(callback, url) {
    $.ajax({
        url: url, dataType: 'json',
        success: callback
    });
}

function myCallBack(result) {
    console.log(result);
}

foo(myCallBack(), '../functions/getcourses.php');

Unfortunatly I can not seem to understand what I am doing wrong.

Smoothal
  • 45
  • 1
  • 11

0 Answers0