简体   繁体   English

如何将2D JavaScript数组发布到服务器?

[英]How can I post a 2D JavaScript array to the server?

I have an array, myData=[[1,2,3],[4,5,6],....,[..,..,..]] I want to post this to the server. 我有一个数组,myData = [[1,2,3],[4,5,6],....,[.......]]我想将其发布到服务器。

Usually, for a JS variable, I just put the variable into a textbox and then submit the form using JS. 通常,对于JS变量,我只是将变量放入文本框,然后使用JS提交表单。 However, when I put the 2D array into a textbox, JS converts it to a string, such that it becomes a 1D array, which looks like: [1,2,3,4,5,6....] 但是,当我将2D数组放入文本框时,JS将其转换为字符串,这样它就变成了1D数组,看起来像:[1,2,3,4,5,6 ....]

I want to be able to post the entire 2D array to the server and retrieve it on the next page using PHP. 我希望能够将整个2D数组发布到服务器,并使用PHP在下一页上检索它。 How do I do that? 我怎么做?

Thanks! 谢谢!

You should use JSON to turn the array into a string with javascript and then parse the string back into an array in php. 您应该使用JSON使用javascript将数组转换为字符串,然后将其解析为php中的数组。

for example: 例如:

var myData = var data = [['hooray',1],['test','meow'],[0,3,2]];
var myData_string = JSON.stringify(myData);

This will turn your object/array into a string which you can then POST and parse with php like so: 这会将您的对象/数组转换为字符串,然后可以通过POST并使用php进行解析,如下所示:

$myData = json_decode($input);

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM