开发者

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. I would like it to post in this format lead_gen[email], lead_gen[address] and so on开发者_如何学Go...

How does one format the Post method to do this. 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. 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.com/serialize/

data: $('#lead_gen_form').serialize(),
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜