简体   繁体   English

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

[英]How to call function written in main file into ajax file in php & jquery

在主文件 (index.php) 中编写了一个 Jquery 函数,并希望在从主文件调用的 ajax 文件中调用相同的函数。

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如果主页面中的函数并且你将ajax注入其中的容器,则该函数需要可以调用,在ajax文件的末尾添加function() //main函数

you can also choose another approach after the ajax success you can call the function directly... by passing it as callback for example:您还可以在 ajax 成功后选择另一种方法,您可以直接调用该函数......通过将其作为回调传递,例如:

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 () {

        }

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM