使用GET将JavaScript数组发送到PHP脚本的最佳方法是什么? [英] What's the best way to send JavaScript array to PHP script using GET?

查看:71
本文介绍了使用GET将JavaScript数组发送到PHP脚本的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我目前正在考虑使用诸如base64编码之类的东西来序列化所有JS对象,而仅使用诸如此类的东西:

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

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

但是,有些事情告诉我,必须有一些比用JavaScript编写自己的base64编码函数更优雅的方法. JavaScript中是否已经内置可以完成这项工作的东西?

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?

更新:PHP中的解码不是问题.我知道如何在PHP中解码base64,JSON.问题是如何在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.

AJAX是不可能的.它必须是带有重新加载页面的干净GET或POST.

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

推荐答案

json_decode()在这里可以为您服务.

json_decode() should serve you well here.

如果仅将包含javascript对象的JSON表示形式的字符串附加到HTTP请求,则可以在PHP中对其进行json_decode()并准备好使用PHP对象.

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字符串的链接,它将您的JS对象转换为必要的JSON.

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

这篇关于使用GET将JavaScript数组发送到PHP脚本的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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