jQuery Conditional statement inside ajax data

ajax, jquery

Solution

You can do this, but not with the syntax you have. You need to create the object first, then use the condition statement separately, like this:

var data = {
    'id': 3,
    'device': $("#ipole4").val(),
    'name': $("#ipole5").val(),
    'ip': $("#ipole6").val(),
    'method': $("#ipole7").val() 
};

if (2 == 2)
    data.info = 2;

$.ajax({
    url:'ajax.php',
    type:'POST',
    data: data
}

Problem

Can I make something like this for example? ``` $.ajax({ url:'ajax.php', type:'POST', data: { id: 3, device: $("#ipole4").val(), name: $("#ipole5").val(), ip: $("#ipole6").val(), method: $("#ipole7").val(), if (2 == 2) { 'info':2 } }, }) ``` I just want to send something more in the special case.

Original source