声明一个Ajax函数并用不同的变量填充它

I'm trying to make my page more efficient by using a separated ".js" file and trying to declare multilple used functions only one time. So I have to declare them in a way, that they caa be used for different situations. For Example passing different data.

Here is my Ajax Function in the "functions.js" file:

function CallAjax(type, url, data, div){

$.ajax({
        type: type,
        url: url,
        data: data,
        success: function (data) {
            console.log(data);
            $(div).html(data);
        }
    });
}

Here is my Code in my PHP File where I use this function and pass Parameters:

CallAjax('POST', 'about.php', '{ aufid : id }', '#con2');

My Problem is the "data" section. How can I declare it? The way I'm doing it doesn't work.

I don't want to use a new Ajax Function everytime when I need different data... I'm trying to trigger less functions as possible.

If you have any tips to make the page more efficient by trying to use less code, then it would be awesome if you mention them, too! Thank you!

you can do it like this:

var obj = {
aufid: 1
};

CallAjax('POST', 'about.php', obj, '#con2');

I propose js callback:

function CallAjax(type, url, data, div){
  $.ajax({
    type: type,
    url: url,
    data: data,
    success: function (data) {
        callback(data);
    }
  });
}

var obj = {
    id:1
};

CallAjax('POST', 'about.php', obj, function(response){
    $(div).html(response);  //or other
});

or a more elegant way in promise:

function CallAjax(type, url, data){
  return $.ajax({
    type: type,
    url: url,
    data: data,
  });
}

var obj = { id: 1 };
var jxhr = CallAjax('POST', 'about.php', obj);

jxhr.done(function(response){
    //successful callback goes here...
}).fail(function(response){
    //failure callback goes here...
}).always(function(response){
    //always callback goes here...
});

: )