简体   繁体   English

来自JavaScript和jQuery中的AJAX回调的外部方法

[英]External method from an AJAX callback in JavaScript & jQuery

I have a function in JS & jQuery that fires an AJAX call and it has a callback block to let me know when it's finished: 我在JS和jQuery中有一个函数来激活一个AJAX调用,它有一个callback块让我知道它什么时候完成:

function ajaxCall(url, type, dataType, dataToSend, callback) {

    if (dataType == undefined) dataType = "json";
    if (dataToSend == undefined) dataToSend = null;

    $.ajax({
        url: url,
        type: type,
        dataType: dataType,
        contentType: "application/json",
        data: dataToSend,
        async: true,
        success: function (result) {
            callback(result);
        },
        error: function (data, status) {
            console.error("Server Error: " + status);
        }
    });
}

I am accessing it like so, but using external functions like showAjaxLoader() just doesn't work! 我这样访问它,但使用外部函数如showAjaxLoader()只是不起作用! it says this function is undefined: 它说这个函数是未定义的:

function registerUser(data) {

    ajaxCall(pathServiceRegister, "POST", undefined, JSON.stringify(data), function (result) {

        // SOME CODE THAT RUNS WHEN IT'S COMPLETE

        // External method:
        showAjaxLoader(false); // Doesn't work

    });
});

function showAjaxLoader(show) {
    var loader = $('.ajax-loader');
    if (show) {
        loader.fadeIn("fast");
    } else {
        loader.fadeOut("fast");
    }
}

What am I doing wrong? 我究竟做错了什么?

Thanks :) 谢谢 :)

Have you tried to do something like: 你有没有尝试过这样的事情:

var that = this;

function registerUser(data) {

    ajaxCall(pathServiceRegister, "POST", undefined, JSON.stringify(data), function (result) {

        // SOME CODE THAT RUNS WHEN IT'S COMPLETE

        // External method:
        that.showAjaxLoader(false); 

    });
});

Worked out some sample. 制定了一些样本。 this may be good practice. 这可能是一种很好的做法。 Try this : 试试这个 :

    $(document).ready(function() {
        $("button").click(function() {registerUser();});
    });

    var Scallback = function(arg) {
        alert("Success :"+arg);
        showAjaxLoader(true);
    }
    var Ecallback = function(arg) {
        alert("Err :"+arg);
        showAjaxLoader(true);
    }

    function showAjaxLoader(show) {
        var loader = $('.ajax-loader');
        if (show) {
            loader.fadeIn("fast");
        } else {
            loader.fadeOut("fast");
        }
    }

    function ajaxCall(url, type, Scallback, Ecallback) {
        $.ajax({
            url : url,
            type : type,
            async : true,
            success : function(result) {
                Scallback(result);
            },
            error : function(data) {
                Ecallback(data)
            }
        });
    }

    function registerUser() 
    {
        ajaxCall(pathServiceRegister, "GET", Scallback, Ecallback);
    }

Declare your method like this 像这样声明你的方法

var obj = {
showAjaxLoader : function(show) {
    var loader = $('.ajax-loader');
    if (show) {
        loader.fadeIn("fast");
    } else {
        loader.fadeOut("fast");
    }
}
}

Then inside ajax, call obj.showAjaxLoader(false); 然后在ajax中,调用obj.showAjaxLoader(false); This may work. 这可能有效。

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

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