if/else语句在.ajax中

Jür*_*aul 0 javascript jquery if-statement

假设我有这段代码通过ajax获取最新帖子:

  $.ajax({
    url: loadmore,
    data: {lastid: lastid,mode:'latest'},
    dataType: 'json',
    type: 'POST',
    timeout: 10000,
    success: function(json){
      //some code
    },
    error: function(jqXHR, textStatus, errorThrown){
      //some code
    }
  });
Run Code Online (Sandbox Code Playgroud)

如何更改数据内容?这是我的尝试,但有些事情失败了.

  $.ajax({
    url: loadmore,
    if($('.postlist').hasClass('best'))
      data: {lastrank: lastrank,mode: 'best'},
    else
      data: {lastid: lastid,mode:'latest'},
    dataType: 'json',
    type: 'POST',
    timeout: 10000,
    success: function(json){
      //some code
    },
    error: function(jqXHR, textStatus, errorThrown){
      //some code
    }
  });
Run Code Online (Sandbox Code Playgroud)

Rod*_*ist 5

三元运营商:

 $.ajax({
    url: loadmore,
    data: ($('.postlist').hasClass('best') ?
      {lastrank: lastrank,mode: 'best'} :
      {lastid: lastid,mode:'latest'}),
    dataType: 'json',
    type: 'POST',
    timeout: 10000,
    success: function(json){
      //some code
    },
    error: function(jqXHR, textStatus, errorThrown){
      //some code
    }
  });
Run Code Online (Sandbox Code Playgroud)