如何从ajax回调传递参数到委托函数

时间:2022-12-08 19:57:40

I'm developing a prototype in jQuery for using ajax call simpler.

我正在使用ajax调用simpler在jQuery中开发一个原型。

Code

 ajax: {
    call : function(url,dataToSend,doOnSuccess,doOnFailure) {
        $.ajax({
            type: "POST",
            url: url,
            data: dataToSend
            ,
            //cache: false,
            success: function (result) {                 

                    doOnSuccess();

            },
            error: function(xhr, textStatus, errorText) {

                    doOnFailure();

            }
        });

    }
}

prototype

ajax.call("MyUrl",{data:"MyData To Send"},alert(),alert());

For example I want to show result of success in alert. So how can I pass ajax callbacks parameters to My delegates(e.g doOnSuccess() and doOnFailure()) which I passed as parameter?

例如,我想在警报中显示成功的结果。那么如何将ajax回调参数传递给我作为参数传递的My委托(例如doOnSuccess()和doOnFailure())?

Best regards!!

2 个解决方案

#1


0  

if return callback Ajax read when or deferred in jquery:

如果返回回调Ajax在jquery中读取或延迟时读取:

jQuery.when

jQuery.deferred

Or Call function e.g:

或呼叫功能,例如:

ajax.call("MyUrl",{data:"MyData To Send"},alert,alert);

#2


0  

Ajax or not, this is a matter of callback functions in js. You just have to pass the functions as parameters. Lets define your custom doOnSuccess and doOnFailure functions:

是不是Ajax,这是js中的回调函数问题。您只需将函数作为参数传递。让我们定义你的自定义doOnSuccess和doOnFailure函数:

function doOnSuccess(data, textStatus, xhr){
    /* Do something with data, textStatus, xhr */
    alert(data);
}

function doOnFailure(xhr, ajaxOptions, thrownError){
    /* Do something with parameters) */
    alert('error');
}

Then you can define your ajax function:

然后你可以定义你的ajax函数:

 ajax: {
    call : function(url,dataToSend,doOnSuccess,doOnFailure) {
        $.ajax({
            type: "POST",
            url: url,
            data: dataToSend
            ,
            //cache: false,
            success: function (data, textStatus, xhr) {                 
                    /* Do something with data before doOnSuccess */
                    doOnSuccess(data, textStatus, xhr);

            },
            error: doOnFailure
        });

    }
}

Now we can call the ajax function:

现在我们可以调用ajax函数:

ajax.call("MyUrl",{data:"MyData To Send"},doOnSuccess,doOnFailure);

#1


0  

if return callback Ajax read when or deferred in jquery:

如果返回回调Ajax在jquery中读取或延迟时读取:

jQuery.when

jQuery.deferred

Or Call function e.g:

或呼叫功能,例如:

ajax.call("MyUrl",{data:"MyData To Send"},alert,alert);

#2


0  

Ajax or not, this is a matter of callback functions in js. You just have to pass the functions as parameters. Lets define your custom doOnSuccess and doOnFailure functions:

是不是Ajax,这是js中的回调函数问题。您只需将函数作为参数传递。让我们定义你的自定义doOnSuccess和doOnFailure函数:

function doOnSuccess(data, textStatus, xhr){
    /* Do something with data, textStatus, xhr */
    alert(data);
}

function doOnFailure(xhr, ajaxOptions, thrownError){
    /* Do something with parameters) */
    alert('error');
}

Then you can define your ajax function:

然后你可以定义你的ajax函数:

 ajax: {
    call : function(url,dataToSend,doOnSuccess,doOnFailure) {
        $.ajax({
            type: "POST",
            url: url,
            data: dataToSend
            ,
            //cache: false,
            success: function (data, textStatus, xhr) {                 
                    /* Do something with data before doOnSuccess */
                    doOnSuccess(data, textStatus, xhr);

            },
            error: doOnFailure
        });

    }
}

Now we can call the ajax function:

现在我们可以调用ajax函数:

ajax.call("MyUrl",{data:"MyData To Send"},doOnSuccess,doOnFailure);