设置jQuery.ajax()的发布数据时,为什么serialize()与serializeArray()具有相同的效果? [英] Why does serialize() have the same effect as serializeArray() when setting post data for jQuery.ajax()?

查看:78
本文介绍了设置jQuery.ajax()的发布数据时,为什么serialize()与serializeArray()具有相同的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有这个jQuery-AJAX代码和一种形式:

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

<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>

如您所见,我正在使用serializeArray(),但是当我使用serialize()时,它也一样..

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

为什么在两种情况下都一样?我应该使用哪些?

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

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

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

推荐答案

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

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

如果传递了字符串( .serialize() 返回),它不会做任何进一步的操作

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

...因此它们在传递时与data属性具有相同的效果. 您可以在此处找到相关的支票:

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

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

您应该使用哪个? 真的在这里没关系, .serialize() ,因为它的键入要少得多.

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.

这篇关于设置jQuery.ajax()的发布数据时,为什么serialize()与serializeArray()具有相同的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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