douhui9380
2015-06-23 07:58 阅读 34

如何将主文件中写入的函数调用到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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanjiong5686 duanjiong5686 2015-06-23 08:08

    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 () {
    
            }
    
        });
    }
    
    点赞 评论 复制链接分享

相关推荐