简体   繁体   English

如何在AJAX成功函数中传递额外的数据?

[英]How to pass extra data in AJAX success function?

I have a function: 我有一个功能:

function Activity() {
    this.LoadFile = function (path, onSuccess) {
        $.ajax({
            url: path,
            dataType: 'html',
            success: onSuccess
        });
    };
}

At the moment I call it like this: 目前,我这样称呼它:

var activity = new Activity();

activity.LoadFile('../file.htm', function (data) {
    console.log(data);
});

What I would like to do, is to add some default values in the AJAX success callback. 我想做的是在AJAX成功回调中添加一些默认值。 Lets say that for example, I always want to log the data by default, without writing it every time I do a LoadFile . 可以说,例如,我一直希望默认情况下记录数据,而不是每次执行LoadFile时都将其写入。 Hope you get the point, thanks in advance. 希望您明白这一点,在此先感谢。

You could make a wrapper function that logs/etc then calls the onSuccess function: 您可以创建一个包装器功能,该功能记录/ etc然后调用onSuccess函数:

this.LoadFile = function (path, onSuccess) {
    var onSuccessWrapper = function(data) {
      console.log(data)
      onSuccess(data)
    }
    $.ajax({
        url: path,
        dataType: 'html',
        success: onSuccessWrapper
    })
}

you could do the folowing 你可以做下面的事情

$.ajax({
    url: path,
    dataType: 'html',
    success: function(data, textStatus, jqXHR) {
        console.log(data);
        onSuccess(data, textStatus, jqXHR);
    }
});

Hi have you tried this? 嗨,你尝试过这个吗?

 $.ajax({
        url: path,
        dataType: 'html',
        success: function(data){
            onSuccess(data);
            alert(data);//default here
        }
    });

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

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