I am passing serialized data and a variable as well to php, but i am getting an unexpected token '(' error in the data section
if i take out the 'lid':userID from the data section, it works fine.
$("#cname").submit(function (e) {
e.preventDefault();
$.ajax({
url: 'someurl',
crossDomain: true,
type: 'post',
data: {$("#cname").serialize(), 'lid':userID},
success: function (data) {
$("#result").html(data);
$.mobile.changePage($('#page-tasklist'));
},
});
});
The output of the .serialize()
method is a string like action=login&UserName=&Password=&Site=Test
. So try this instead.
$("#cname").submit(function (e) {
e.preventDefault();
$.ajax({
url: 'someurl',
crossDomain: true,
type: 'post',
data: $("#cname").serialize() + '&lid=' + userID,
success: function (data) {
$("#result").html(data);
$.mobile.changePage($('#page-tasklist'));
},
});
});
Also, if you just remove lid: userID
from your ajax request, you would actually still get an error because {$("#cname").serialize()}
is not valid json. The data
parameter can accept either an encoded string like what .serialize()
returns or a json object.
The following 2 examples are equivalent:
$.ajax({url:'index.php',type:'POST',data:{z:1,y:2}});
$.ajax({url:'index.php',type:'POST',data:'x=1&y=2'});
Try add to variable and then push new key/val.
$("#cname").submit(function (e) {
var serializeData = $("#cname").serializeArray();
serializeData.push({'lid':userID})
e.preventDefault();
$.ajax({
url: 'someurl',
crossDomain: true,
type: 'post',
data: serializeData,
success: function (data) {
$("#result").html(data);
$.mobile.changePage($('#page-tasklist'));
}
});
});