【问题标题】:What is the best way to add extra data to a jquery ajax post (in addition to a serialized form)?将额外数据添加到 jquery ajax 帖子(除了序列化表单)的最佳方法是什么?
【发布时间】:2014-01-13 04:48:09
【问题描述】:

我有以下代码:

 $.post('/Calendar/Add', $("#calendarForm").serialize(), function (data) {

 });

除了#calendarForm 中的所有项目之外,我还想添加另一条数据。

假设我想在查询字符串中附加一个额外的键值对:

  personId=223

我宁愿不要在 calendarForm 中添加额外的隐藏输入,因为这就是我现在正在做的事情,而且有点混乱。当我调用这篇文章时,除了 calendarForm 中的所有值之外,还有什么简单的方法可以添加额外的数据吗?

我尝试过这样的事情:

 $.post('/Calendar/Add', $("#calendarForm").serialize() + "&personId=223", function (data) {

 });

但这似乎不起作用?

【问题讨论】:

标签: jquery ajax forms post


【解决方案1】:

尝试使用serializeArray

var data = $('#myFormName').serializeArray();
data.push({name: 'myParamName', value: 'MyParamValue'});

更新1:

您可以在$.post中使用以下代码:

$.post('/Calendar/Add', data, function (data)    
{});

更多信息请查看this

【讨论】:

  • 只传入“data”而不是“$("#calendarForm").serialize() + "&personId=223"" ??
  • @leora 是的,这是推荐的想法。因为.serialize() 会返回一个字符串。来吧。
  • 不需要先把数据数组转成字符串吗?
【解决方案2】:

你可以这样做

$.post('/Calendar/Add', { personId: 223,calendarForm: $("#calendarForm").serialize() },
         function (data) { });

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 2011-03-09
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2011-01-15
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多