最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a hash post using jQuery Post method - Stack Overflow

programmeradmin1浏览0评论
$.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
Add a ment  | 

4 Answers 4

Reset to default 2

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./serialize/

data: $('#lead_gen_form').serialize(),
发布评论

评论列表(0)

  1. 暂无评论