$.ajax({
url: '/create_lead',
data: {
name: $('#lead_gen_name').val(),
lead_gen[email]: $('#lead_gen_email').val(),
},
type: 'POST',
dataType: 'json',
success: function(data) {
}
});
I would like to use the jQuery Post method to post a hash of information. I would like it to post in this format lead_gen[email], lead_gen[address] and so on...
How does one format the Post method to do this. The above code fails with a syntax error.
$.ajax({
url: '/create_lead',
data: {
name: $('#lead_gen_name').val(),
lead_gen[email]: $('#lead_gen_email').val(),
},
type: 'POST',
dataType: 'json',
success: function(data) {
}
});
I would like to use the jQuery Post method to post a hash of information. I would like it to post in this format lead_gen[email], lead_gen[address] and so on...
How does one format the Post method to do this. The above code fails with a syntax error.
Share Improve this question asked Oct 3, 2011 at 18:04 JZ.JZ. 22k33 gold badges119 silver badges193 bronze badges 1- You've also got a dangling ma at the end of that lead_gen line. – Jerod Venema Commented Oct 3, 2011 at 18:12
4 Answers
Reset to default 2Assuming your server can handle it, you can use nested objects in your call:
data: {
name: $('#lead_gen_name').val(),
lead_gen: { email: $('#lead_gen_email').val(), address: "1234 W Street" }
}
Convert it to JSON before the post. That is, put all the data in a javascript object, make a call to JSON.stringify()
, then put the resulting string in your data
section of the ajax call.
It looks to me like it should work, but for the fact that lead_gen[email]: won't work as a key the way you have it here. Put quotes around it.
$.ajax({
url: '/create_lead',
data: {
name: $('#lead_gen_name').val(),
'lead_gen[email]': $('#lead_gen_email').val(),
},
type: 'POST',
dataType: 'json',
success: function(data) { }
});
You might be able to use jQuery.serialize to do what you want. http://api.jquery./serialize/
data: $('#lead_gen_form').serialize(),