简体   繁体   English

将带有 arguments 的成功回调 function 传递给另一个执行 ZA34A6659BCEAE779F28185E757 调用的 function

[英]Pass success callback function with arguments to another function that performs AJAX call

I'm new to javascript and trying to understand if following thing is possible:我是 javascript 的新手,并试图了解以下事情是否可能:

Let's say I use following ajax request construction in several places in my code (with different success callback functions - in this example only handleSuccessCallback() ) and different url and data request param:假设我在我的代码中的几个地方使用了以下 ajax 请求构造(具有不同的成功回调函数 - 在本示例中只有handleSuccessCallback() )和不同的 url 和数据请求参数:

  jQuery.ajax({
        type: 'GET',
        url: loadUrl,
        data: data,
        error: errorCallback,
        success: function(response) {
            this.handleSuccessCallback(response);
        }
    }); 

function handleSuccessCallback(response) {
     Do something with response here...
}

I wanted to refactor this code in such way, that I don't need to create construction above each time I need to send request.我想以这种方式重构这段代码,这样我就不需要在每次需要发送请求时都在上面创建构造。 I wanted to create a function in my utils class that will take url, request data and error/success callback.我想在我的实用程序 class 中创建一个 function,它将采用 url、请求数据和错误/成功回调。 Something like this:像这样的东西:

 function sendAjaxRequest(url, data, errorCallback, successCallback)
  jQuery.ajax({
        type: 'GET',
        url: url,
        data: data,
        error: errorCallback,
        success: successCallback
  }); 
 }

Then, I wanted to call this function several times passing different arguments from my main script.然后,我想多次调用这个 function 从我的主脚本传递不同的 arguments。

In my main code:在我的主要代码中:

var url = "https://some url";
var data = {};
var errorCallback = handleErrorCallback();
var successCallback = handleSuccessCallback(); // ??? This function requires `response` argument being passed, how it can be done?

utils.sendAjaxRequest(url, data, errorCallback, successCallback);


function handleSuccessCallback(response) {
     Do something with response here...
}

function handleErrorCallback() {
     Do something with error here...
}

However, what I don't understanding is the following: how can I pass success callback function ( handleSuccessCallback(response) ) to the sendAjaxRequest() function, if my handleSuccessCallback(response) needs params which is result of the success ajax callback itself( response )? However, what I don't understanding is the following: how can I pass success callback function ( handleSuccessCallback(response) ) to the sendAjaxRequest() function, if my handleSuccessCallback(response) needs params which is result of the success ajax callback itself( response )?

How can I pass this response param to handleSuccessCallback() when passing it to sendAjaxRequest() if I don't know yet what will be the response from ajax call?如果我还不知道 ajax 调用的response是什么,那么在将它传递给handleSuccessCallback()时如何将这个response参数传递给sendAjaxRequest()

When you want to call:当你想打电话时:

function sendAjaxRequest(url, data, errorCallback, successCallback)

You just need to give names of functions for errorCallback and successCallback .您只需要给出errorCallbacksuccessCallback的函数名称。 You are passing function references, so they can be called by other code.您正在传递 function 引用,因此它们可以被其他代码调用。 The params are provided at the point where those functions are called, like in your original example:参数是在调用这些函数的地方提供的,就像在您的原始示例中一样:

success: function(response) {
        this.handleSuccessCallback(response);
    }

Here the success function as defined in ajax with first parameter as response , so you have an anonymous function here with the first parameter as response .这里成功 function 定义在ajax中,第一个参数作为response ,所以你有一个匿名 function 这里第一个参数作为response

So you just need to define your functions to match what they will be when they get called by ajax , eg:所以你只需要定义你的函数来匹配它们被ajax调用时的样子,例如:

var firstSuccessCallback = function(response) {
    //do something;
}

var firstErrorCallback = function(error) {
    //do something;
}

you can do:你可以做:

sendAjaxRequest(url, data, firstErrorCallback, firstSuccessCallback)

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

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