Let's say I have this code which gets the latest posts via 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
}
});
How do I change the content of the data? Here's my try but something failed.
$.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
}
});
Let's say I have this code which gets the latest posts via 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
}
});
How do I change the content of the data? Here's my try but something failed.
$.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
}
});
Share
Improve this question
asked Feb 18, 2012 at 8:08
Jürgen PaulJürgen Paul
15k28 gold badges96 silver badges136 bronze badges
2 Answers
Reset to default 5Ternary operator:
$.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
}
});
You could use the ?: (ternary operator) operator:
data: ($('.postlist').hasClass('best')) ?
{lastrank: lastrank,mode: 'best'} :
{lastid: lastid,mode:'latest'},