发布 jquery .serializeArray(); 通过ajax输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4862189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:03:39  来源:igfitidea点击:

posting jquery .serializeArray(); output through ajax

jqueryajaxserializearray

提问by Hailwood

Quick question

快速提问

If I have serialized a form using jquery's .serializeArray();function do I need to do anything to it before I can send it off using jquery's ajax data:?

如果我使用 jquery 的.serializeArray();函数序列化了一个表单,我是否需要对它做任何事情才能使用 jquery 的 ajax 发送它data:

e.g. can I send off

例如我可以送行吗

[{name: inp1, value: 'val1'}, {name: inp2, value: 'val2'}]as is, or do I need to preprocess it somehow?

[{name: inp1, value: 'val1'}, {name: inp2, value: 'val2'}]照原样,还是我需要以某种方式对其进行预处理?

and, in php how would I read this?

而且,在 php 中我将如何阅读?

回答by lonesomeday

It would be better here to use serialize. This converts your form's values into a simple string that can be used as the AJAX call's dataattribute:

在这里使用会更好serialize。这会将表单的值转换为一个简单的字符串,可以用作 AJAX 调用的data属性:

var myData = $('#yourForm').serialize();
// "inp1=val1&inp2=val2"
$.ajax({
    url: "http://example.com",
    data: myData
});

Presuming you send this to PHP using the GETmethod, you can access these values using $_GET['inp1']and $_GET['inp2']

假设您使用该GET方法将其发送到 PHP ,您可以使用$_GET['inp1']和访问这些值$_GET['inp2']



Edit: You can convert an array made by serializeArrayinto a parameter string using $.param

编辑:您可以serializeArray使用$.param

var myData = $('#yourForm').serializeArray();
// remove items from myData
$.ajax({
    url: "http://example.com",
    data: $.param(myData) // "inp1=val1&inp2=val2"
});