问题描述
我正在尝试将表单发送给控制器. 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.
两个版本均可用的原因是,当您只想发出HTTP请求(将结果放入查询字符串中)时,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()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!