.serialize() 和 .serializeArray() 有什么区别? [英] What's the difference between .serialize() and .serializeArray()?

查看:14
本文介绍了.serialize() 和 .serializeArray() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向控制器发送表单.jQuery 文档说 .serializeArray() 应该发送一个 json 数组,而 .serialize() 应该创建一个查询字符串.

I'm experimenting with sending a form to a controller. jQuery documentation says that .serializeArray() should send a json array, and .serialize() should create a query string.

但是,当我尝试使用 IE9 F12 模式进行检查时,在这两种情况下,它看起来都像一个查询字符串.我打过哪个电话...

However, when I try it, and inspecting with IE9 F12-mode, it looks like a query string, in both cases. Which ever call I make...

我错过了什么?

推荐答案

serializeArray 创建一个数组(不是一个json数组"——没有这样的东西);您可以使用 console.log($("#myform").serializeArray()) 自行测试.另一方面,serialize 创建一个查询字符串,该字符串是 HTTP 请求的一部分.这两种表示在某种意义上是等效的,即使用适当的代码可以将一种转换为另一种而不会产生任何歧义.

serializeArray creates an array (not a "json array" -- there is no such thing); you can test this yourself with console.log($("#myform").serializeArray()). On the other hand, serialize creates a query string that's meant to be part of an HTTP request. Both representations are equivalent in the sense that using appropriate code you can convert one to the other without any ambiguity.

两个版本都可用的原因是 serialize 当你只想发出 HTTP 请求时更方便(只需将结果放在查询字符串中)而 serializeArray 如果你想自己处理结果会更方便.

The reason for both versions being available is that serialize is more convenient when you just want to make an HTTP request (just put the result in the query string) while serializeArray is more convenient if you want to process the results yourself.

jQuery 的 AJAX 方法不在乎您是否给它们一个或另一个,因为它们会检测参数的类型并将其转换为查询字符串(如果它已经不是一个),因此当请求是在外部观察者之外发出时无法告诉参数的原始格式是什么.

jQuery's AJAX methods don't care if you give them one or the other because they detect the type of the parameter and convert it to a query string if it's not one already, so by the point the request is made outside observers cannot tell what was the original format of the parameters.

这篇关于.serialize() 和 .serializeArray() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆