如何将额外数据传递给ajax成功事件?

时间:2012-11-14 14:00:31

标签: ajax jquery

如何将额外数据传递给ajax成功事件?

var extra_data = 'some data';
var a = {
    type : 'post',
    async : true,
    cache : false,
    dataType : 'json',
    timeout : 15000,
    contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
    global : true,
    url : url,
    success : [
        function(response, textStatus, jqXHR, extra_data){

        }
    ]
};

1 个答案:

答案 0 :(得分:0)

如果我记得(之前没有var)你可以将extra_data声明为全局变量,你可以在success方法的范围内使用它,而不需要通过方法参数传递它

extra_data = 'some data';
var a = {
 type : 'post',
 async : true,
 cache : false,
 dataType : 'json',
 timeout : 15000,
 contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
 global : true,
 url : url,
 success : [
     function(response, textStatus, jqXHR){
       console.log(extra_data); //for example
     }
 ]
};