jQuery.post()使用序列化表单数据和提交中的自定​​义对象

时间:2022-12-05 17:02:43

When doing ajax stuff with jQuery you can pass your data along to the server in two key ways:

使用jQuery执行ajax时,您可以通过两种主要方式将数据传递到服务器:

  1. Use an object like

    使用像这样的对象

    {
     firstname:'blah',
     lastname: 'derp'
    }
    
  2. Use a string like &firstname=blah&lastname=derp

    使用类似&firstname = blah&lastname = derp的字符串

(1) Arises naturally when you're passing in values programmatically.

(1)当你以编程方式传递值时自然会出现。

(2) Arises naturally when you've got input fields.

(2)当你有输入字段时自然会出现。

My problem: it doesn't look like you can combine them, i.e.

我的问题:看起来你不能把它们结合起来,即

jQuery.extend({
 firstname:'blah',
 lastname:'derp'
},jQuery('form.some-form').serialize());

And it's been nagging me for a while.

它一直在唠叨我。

Truthfully I think they just need an objectified version of the serialize method that just gives you a json representation.

说实话,我认为他们只需要一个客观化版本的序列化方法,它只是给你一个json表示。

1 个解决方案

#1


1  

This previous discussion should solve the problem of converting from json to a query string, and vice-versa:

前面的讨论应该解决从json转换为查询字符串的问题,反之亦然:

How can I convert query string or JSON object map to single JSON object with jQuery?

如何使用jQuery将查询字符串或JSON对象映射转换为单个JSON对象?

Also, you might want to have a look at the jQuery Form plugin

另外,您可能希望查看jQuery Form插件

#1


1  

This previous discussion should solve the problem of converting from json to a query string, and vice-versa:

前面的讨论应该解决从json转换为查询字符串的问题,反之亦然:

How can I convert query string or JSON object map to single JSON object with jQuery?

如何使用jQuery将查询字符串或JSON对象映射转换为单个JSON对象?

Also, you might want to have a look at the jQuery Form plugin

另外,您可能希望查看jQuery Form插件