3

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.

1 Answer 1

11

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.