jQuery 如何使用 XMLHttpRequest 将数组发送到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12096941/
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 11:18:52  来源:igfitidea点击:

How to send arrays using XMLHttpRequest to server

jqueryajaxxmlhttprequest

提问by Esailija

As I know using ajax you can send data to the server but I'm confused about sending an array to post using XMLHttpRequestnot any library like jQuery. My question is that, is that possible to send an array to phpusing XMLHttpRequestand how does jQuerysend an array to php, I mean does jQuery do any additional work to send an array to server (php $_POST) ?

据我所知,使用 ajax 可以将数据发送到服务器,但我对发送数组以使用XMLHttpRequest不使用任何库(如 jQuery)进行发布感到困惑。我的问题是,是否可以将数组发送到phpusingXMLHttpRequest以及如何jQuery将数组发送到 php,我的意思是 jQuery 是否做了任何额外的工作来将数组发送到服务器(php $_POST)?

回答by Esailija

Well you cannot send anything but a string of bytes. "Sending arrays" is done by serializing (making string representation of objects) the array and sending that. The server will then parse the string and re-build in-memory objects from it.

好吧,除了一串字节之外,您不能发送任何内容。“发送数组”是通过序列化(使对象的字符串表示)数组并发送它来完成的。然后服务器将解析字符串并从中重建内存中的对象。

So sending [1,2,3]over to PHP could happen like so:

所以发送[1,2,3]到 PHP 可能会像这样:

var a = [1,2,3],
    xmlhttp = new XMLHttpRequest;

xmlhttp.open( "POST", "test.php" );
xmlhttp.setRequestHeader( "Content-Type", "application/json" );
xmlhttp.send( '[1,2,3]' ); //Note that it's a string. 
                          //This manual step could have been replaced with JSON.stringify(a)

test.php:

测试.php:

$data = file_get_contents( "php://input" ); //$data is now the string '[1,2,3]';

$data = json_decode( $data ); //$data is now a php array array(1,2,3)

Btw, with jQuery you would just do:

顺便说一句,使用 jQuery,您只需执行以下操作:

$.post( "test.php", JSON.stringify(a) );

回答by PinnyM

That depends on the protocol you choose to package your data structure. The 2 most commonly used are XML and JSON. Both have ways to declare an array:

这取决于您选择打包数据结构的协议。最常用的两种是 XML 和 JSON。两者都有声明数组的方法:

JSON: ['one thing', 'another thing']

JSON: ['one thing', 'another thing']

XML: <things><thing name='one thing' /><thing name='another thing' /></things>

XML: <things><thing name='one thing' /><thing name='another thing' /></things>

and neither will take any significant extra work by the server. In many cases it will actually reduce work since you don't need to use a naming convention to differentiate between them.

并且两者都不会给服务器带来任何重要的额外工作。在许多情况下,它实际上会减少工作量,因为您不需要使用命名约定来区分它们。

回答by Hanlet Esca?o

You want to send a jSon object (which can be an array). Check this out if you are using php Send JSON data to PHP using XMLHttpRequest w/o jQuery.

您想发送一个 json 对象(可以是一个数组)。如果您正在使用 php Send JSON data to PHP using XMLHttpRequest w/o jQuery,请检查一下。

More about jSon: http://json.org/

更多关于 json:http://json.org/

jQuery jSon sample: JQuery and JSON

jQuery json 示例:JQuery 和 JSON