如何将主文件中写入的函数调用到php和jquery中的ajax文件中

问题描述:

Written a Jquery function in main file (index.php) & want to call same function in ajax file that is called from main file.

if the function in the main page and you inject ajax to container inside it, the function need to be available for calling, in the end of the ajax file add function() //main function

you can also choose another approach after the ajax success you can call the function directly... by passing it as callback for example:

ajax(url,mainFunction,true)


function ajax(url, callback, async) {
    var date = new Date();
    var ticks = date.getTime();

    if (url.indexOf('?') == -1)
        url = url + "?tick=" + ticks;
    else
        url = url + "&tick=" + ticks;


    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'html',
        async: async == undefined ? true : async,
        success: function (data) {

            if (data != undefined)
                callback(data);
        },

        error: function (xhr, textStatus, errorThrown) {
           //throw error
        },

        complete: function () {

        }

    });
}