使用GET将JavaScript数组发送到PHP脚本的最佳方法是什么?

时间:2021-01-09 02:47:20

I have an interactive web application powered by jQuery where users can manipulate visual objects on the screen. When done, the "state" of JavaScript objects should be sent to PHP to store into database. I'd prefer to use GET for this, but solution that uses POST to submit the data is also viable.

我有一个由jQuery驱动的交互式Web应用程序,用户可以在屏幕上操作可视对象。完成后,应将JavaScript对象的“状态”发送到PHP以存储到数据库中。我更喜欢使用GET,但使用POST提交数据的解决方案也是可行的。

I'm currently thinking to serialize all JS objects using something like base64 encoding and just use something like:

我目前正在考虑使用类似base64编码的方法序列化所有JS对象,并使用类似的东西:

var str = encode_objects();
document.location = 'result.php?result='+str;

However, something tells me there has to be some, more elegant, way than writing my own base64 encoding function in JavaScript. Isn't there something already built into JavaScript that can do the job?

但是,有些东西告诉我必须有一些更优雅的方式,而不是在JavaScript中编写我自己的base64编码函数。是不是已经内置到JavaScript中的东西可以完成这项工作?

Update: decoding in PHP is not the problem. I know how to decode base64, JSON, whatever in PHP. The problem is how to encode data on JavaScript side.

更新:PHP中的解码不是问题。我知道如何在PHP中解码base64,JSON。问题是如何在JavaScript端编码数据。

AJAX is out of the question. It has to be a clean GET or POST with page reload.

AJAX是不可能的。它必须是一个干净的GET或POST与页面重新加载。

6 个解决方案

#1


json_decode() should serve you well here.

json_decode()应该在这里为你服务。

If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.

如果您只是将包含javascript对象的JSON表示的字符串附加到HTTP请求中,则可以在PHP中使用json_decode()并准备好PHP对象。

Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.

编辑:我应该提到这个页面有一个链接到Javascript JSON字符串,它将您的JS对象转换为必要的JSON。

#2


first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.

首先,如果你正在更新对象状态,它应该是一个POST。尽量保持你所有的GET幂等。

second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.

第二,即使你不想做任何AJAX,JSON仍然是你的朋友。最简单的方法是将对象序列化为JSON,并在POST dataload中发送结果字符串。有许多方法可以解码PHP中的JSON,你们都已经完成了设置。

#3


You can use this serialize function, and then unserialize it on the PHP end.

您可以使用此序列化函数,然后在PHP端反序列化它。

#4


GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).

GET或POST可能取决于您的数据大小:您可以通过GET传递的数据有限(大约2000字节;更多,取决于浏览器)。

There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.

还有一件事需要考虑:如果要修改数据,则应使用POST;不是GET,用来...获取......数据。

About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.

关于格式,我真的不会像自定义函数那样做任何像base64这样的东西:我肯定会选择JSON,这是一种原生的Javascript表示法。

You can for instance have a look at :

你可以看一下:

There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)

有许多JS库可以生成JSON(可能每个JS Framework都可以;并且有像这样的standlone库);从PHP 5.2开始,PHP中有函数对其进行编码/解码(参见json_encode和json_decode)

#5


If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.

如果它只是一个简单的平面数组,你不需要做任何花哨的事情,因为PHP有​​一个内置的功能来解析GET / POST变量名的数组语法。下面的粗略示例。

Javascript side:

// Do the parameter-building however you want, I picked the short/messy way
var arrayvalues = [1, 2, 'a'];
var querystring = "var[]=" + arrayvalues.join("&var[]=");
querystring += "&var[something]=abcdef";
// querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"

PHP side:

var_dump($_POST);
// Remember to validate the data properly!
if ( is_array($_POST['var']) ) {
    count($_POST['var']);
    echo $_POST['var']['something'];
    array_map('do_something_interesting', $_POST['var']);
}

#6


I've used Jquery/JSON.

我使用过Jquery / JSON。

var jsonArray = $.toJSON(jsArray);

Then sent it via JQuery/Ajax

然后通过JQuery / Ajax发送它

#1


json_decode() should serve you well here.

json_decode()应该在这里为你服务。

If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.

如果您只是将包含javascript对象的JSON表示的字符串附加到HTTP请求中,则可以在PHP中使用json_decode()并准备好PHP对象。

Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.

编辑:我应该提到这个页面有一个链接到Javascript JSON字符串,它将您的JS对象转换为必要的JSON。

#2


first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.

首先,如果你正在更新对象状态,它应该是一个POST。尽量保持你所有的GET幂等。

second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.

第二,即使你不想做任何AJAX,JSON仍然是你的朋友。最简单的方法是将对象序列化为JSON,并在POST dataload中发送结果字符串。有许多方法可以解码PHP中的JSON,你们都已经完成了设置。

#3


You can use this serialize function, and then unserialize it on the PHP end.

您可以使用此序列化函数,然后在PHP端反序列化它。

#4


GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).

GET或POST可能取决于您的数据大小:您可以通过GET传递的数据有限(大约2000字节;更多,取决于浏览器)。

There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.

还有一件事需要考虑:如果要修改数据,则应使用POST;不是GET,用来...获取......数据。

About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.

关于格式,我真的不会像自定义函数那样做任何像base64这样的东西:我肯定会选择JSON,这是一种原生的Javascript表示法。

You can for instance have a look at :

你可以看一下:

There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)

有许多JS库可以生成JSON(可能每个JS Framework都可以;并且有像这样的standlone库);从PHP 5.2开始,PHP中有函数对其进行编码/解码(参见json_encode和json_decode)

#5


If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.

如果它只是一个简单的平面数组,你不需要做任何花哨的事情,因为PHP有​​一个内置的功能来解析GET / POST变量名的数组语法。下面的粗略示例。

Javascript side:

// Do the parameter-building however you want, I picked the short/messy way
var arrayvalues = [1, 2, 'a'];
var querystring = "var[]=" + arrayvalues.join("&var[]=");
querystring += "&var[something]=abcdef";
// querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"

PHP side:

var_dump($_POST);
// Remember to validate the data properly!
if ( is_array($_POST['var']) ) {
    count($_POST['var']);
    echo $_POST['var']['something'];
    array_map('do_something_interesting', $_POST['var']);
}

#6


I've used Jquery/JSON.

我使用过Jquery / JSON。

var jsonArray = $.toJSON(jsArray);

Then sent it via JQuery/Ajax

然后通过JQuery / Ajax发送它