|
Using a conditional within an ajax request?
Hi
I have an ajax request, and want to set up a conditional within it based on a variable that was sent to the ajax function (inputSwitch), but am not getting the syntax correct. Here's the first part of the request:
$.ajax({
type: "POST",
url: "mypage.php",
data: {post1:var1, post2:var2, post3:var3},
What I want is for the data object to be...
data: {post1:var1, post2:var2},
So..
if (inputSwitch == 'method1') {
data: {post1:var1, post2:var2, post3:var3},
}
else {
data: {post1:var1, post2:var2}
}
...but javascript doesn't seem to like conditionals within ajax requests.
I tried setting up a conditional at the start of the function before the request, putting each object within a var, and then placing the correct var in the data object, but that's no go either.
Can someone please explain the correct syntax, or if it can be done at all?
Thanks for your time and help.
|