为jQuery.ajax()设置post数据时,为什么serialize()与serializeArray()具有相同的效果?

时间:2022-06-24 16:46:24

i have this jQuery-AJAX code below and a form:

我有下面的jQuery-AJAX代码和一个表单:

<script type="text/javascript">
$(document).ready(function () {
    $('form').submit(function () {
        form_data = $(this).serializeArray();

        $.ajax({
            url: "/frontend_dev.php/coche1/update/id/1",
            type: "POST",
            data: form_data

            });
        });
        return false;

});
</script>

As you can see I'm using serializeArray() but when i use serialize() it also works the same..

正如你所看到的,我正在使用serializeArray(),但是当我使用serialize()时,它也可以使用相同的..

Why in both cases works the same? What of them should i use?

为什么两种情况都一样?我应该用什么?

Im using symfony as php framework. I can offer you more info if you need it.

我使用symfony作为PHP框架。如果您需要,我可以为您提供更多信息。

3 个解决方案

#1


16  

If an object/array gets passed (which .serializeArray() returns), it's serialized via $.param().

如果传递了一个对象/数组(.serializeArray()返回),则通过$ .param()对其进行序列化。

If a string get passed (which .serialize() returns) it doesn't do anything further.

如果一个字符串被传递(.serialize()返回)它不会做任何进一步的事情。

...so they have the same effect when passed as the data property. You can find the relevant check here:

...因此它们作为data属性传递时具有相同的效果。你可以在这里找到相关的支票:

    // convert data if not already a string
    if ( s.data && s.processData && typeof s.data !== "string" ) {
        s.data = jQuery.param( s.data, s.traditional );
    }

Which one should you use? It really doesn't matter here, .serialize() makes the same $.param() call, so they do the exact same amount of work. I personally use .serialize() because it's simply less to type.

你应该使用哪一个?这里没关系,.serialize()进行相同的$ .param()调用,所以他们完成相同的工作量。我个人使用.serialize()因为它只是更少的类型。

#2


5  

In this case, they're the same. But there is a big difference if you do not specify the type - serialize will do a GET and serializeArray will do a POST!

在这种情况下,它们是相同的。但是如果你没有指定类型会有很大的不同 - serialize会做一个GET而serializeArray会做一个POST!

#3


0  

I've noticed that in CodeIgniter, the .serialize method can produce data that gets polluted by CodeIgniter's CSRF protection (CodeIgniter somehow adds semi-colons to the array keys), whereas the forms I've submitted with .serializeArray did not have this problem.

我注意到在CodeIgniter中,.serialize方法可以生成被CodeIgniter的CSRF保护污染的数据(CodeIgniter以某种方式将分号添加到数组键),而我用.serializeArray提交的表单没有这个问题。

Also, if you're using complex POST names, e.g. name="some[a][data][structure]" where the places in the $_POST array changes and are not arbitrary, then using .serialize will probably be much easier because you can convert it back into a PHP data structure much more easily.

此外,如果您使用复杂的POST名称,例如name =“some [a] [data] [structure]”$ _POST数组中的位置发生变化并且不是任意的,然后使用.serialize可能会更容易,因为你可以将它转换回PHP数据结构更多容易。

#1


16  

If an object/array gets passed (which .serializeArray() returns), it's serialized via $.param().

如果传递了一个对象/数组(.serializeArray()返回),则通过$ .param()对其进行序列化。

If a string get passed (which .serialize() returns) it doesn't do anything further.

如果一个字符串被传递(.serialize()返回)它不会做任何进一步的事情。

...so they have the same effect when passed as the data property. You can find the relevant check here:

...因此它们作为data属性传递时具有相同的效果。你可以在这里找到相关的支票:

    // convert data if not already a string
    if ( s.data && s.processData && typeof s.data !== "string" ) {
        s.data = jQuery.param( s.data, s.traditional );
    }

Which one should you use? It really doesn't matter here, .serialize() makes the same $.param() call, so they do the exact same amount of work. I personally use .serialize() because it's simply less to type.

你应该使用哪一个?这里没关系,.serialize()进行相同的$ .param()调用,所以他们完成相同的工作量。我个人使用.serialize()因为它只是更少的类型。

#2


5  

In this case, they're the same. But there is a big difference if you do not specify the type - serialize will do a GET and serializeArray will do a POST!

在这种情况下,它们是相同的。但是如果你没有指定类型会有很大的不同 - serialize会做一个GET而serializeArray会做一个POST!

#3


0  

I've noticed that in CodeIgniter, the .serialize method can produce data that gets polluted by CodeIgniter's CSRF protection (CodeIgniter somehow adds semi-colons to the array keys), whereas the forms I've submitted with .serializeArray did not have this problem.

我注意到在CodeIgniter中,.serialize方法可以生成被CodeIgniter的CSRF保护污染的数据(CodeIgniter以某种方式将分号添加到数组键),而我用.serializeArray提交的表单没有这个问题。

Also, if you're using complex POST names, e.g. name="some[a][data][structure]" where the places in the $_POST array changes and are not arbitrary, then using .serialize will probably be much easier because you can convert it back into a PHP data structure much more easily.

此外,如果您使用复杂的POST名称,例如name =“some [a] [data] [structure]”$ _POST数组中的位置发生变化并且不是任意的,然后使用.serialize可能会更容易,因为你可以将它转换回PHP数据结构更多容易。