I am making a post call and want to set the paramaters I send dynamically through some if/else statements. The following simplified version doesn’t work but if I change ‘{postvars}’ to ‘{n: n}’ then it does, even though they’re equivalent, right?
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#clicky").click(function () {
var postvars; var mydata;
var n = 'dave';
postvars = 'n: ' + n;
$.post("test.php",
{postvars},
function(data){
},
"text"
);
$("#test").text(postvars);
});
});
</script>
</head>
<body>
<div id='clicky'>click here</div>
<div id='test'></div>
</body>
</html>
{postvars} and {n: n} are not equivelent,
{postvars} will be seen to javascript as an object initalizer, and will error out as postvars just a string which can not set the attributes/values of an object by itself.
{n: n}, however is a proper object initalizer as it gives name and value
jquery ajax functions take either a name value pair string,
or a json object,