[英]Create a hash post using jQuery Post method
$.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. 我想使用jQuery Post方法发布信息哈希。 I would like it to post in this format lead_gen[email], lead_gen[address] and so on...
我希望它以这种格式发布lead_gen [email],lead_gen [address]等。
How does one format the Post method to do this. 如何格式化Post方法来做到这一点。 The above code fails with a syntax error.
上面的代码因语法错误而失败。
Assuming 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. 在发布之前将其转换为JSON。 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. 也就是说,将所有数据放入javascript对象,调用
JSON.stringify()
,然后将结果字符串放入ajax调用的data
部分。
You might be able to use jQuery.serialize to do what you want. 您可能可以使用jQuery.serialize进行所需的操作。 http://api.jquery.com/serialize/
http://api.jquery.com/serialize/
data: $('#lead_gen_form').serialize(),
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. 在我看来,它应该可以工作,但是lead_gen [email]:不会像您在这里那样拥有钥匙的事实。 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) { }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.