将值传递给成功函数

时间:2017-01-18 21:36:58

标签: javascript jquery

是否可以将值传递给success函数?

function updateData(data_type){
  var current_url = "myUrl";
  var opc = data_type;
  var dataset = [];
  $.ajax({
    data : {'type': opc},
    url : current_url,
    type : 'get',
    success: function(data){
      update(data)
    }
  });
  console.log("update: " + data_type); 
}

success函数中,我想访问我通过参数传递的data_type变量。

这可能吗?

2 个答案:

答案 0 :(得分:1)

直接使用它,如下所示:

function updateData(data_type){
  var current_url = "myUrl";
  var opc = data_type;
  var dataset = [];
  $.ajax({
    data : {'type': opc},
    url : current_url,
    type : 'get',
    success: function(data){
       update(data, data_type);
    }
  });
  console.log("update: " + data_type); 
}

答案 1 :(得分:1)

function updateData( data_type ) {

  var current_url = "myUrl";
  var opc = data_type;
  var dataset = [];

  $.ajax( {
    data : {'type': opc},
    url : current_url,
    type : 'get',
    success: function( data ){
      // `data_type` is available here, use as needed.
      // You can pass it to other functions inside this one as well,
      // i.e. the function below could be `update( data, data_type );`.
      update( data );
    }
  } );

  console.log( "update: " + data_type ); 

}