我有以下功能:
function myFunction () {
$.getJSON('remote.php', function(json) {
var messages = json;
function check() {
...
我在那里调用remote.php脚本,它进行一个简单的选择查询并使用json返回所有数据。
我想向这个名为 time
的查询传递一个参数,该参数将在我的代码中通过以下方式填充:
var actualTime = new Date(params);
我知道在 php 脚本上我必须这样做:
$time = $_POST['time'];
但是我应该如何修改我的 jquery 才能进一步传递这个参数?
最佳答案
只需将一个对象传递给 $.getJSON
。它将作为 $_GET
发送到 PHP。
$.getJSON('remote.php', {
time: actualTime.toJSON()
}, function(json) {
var messages = json;
});
那么您的日期将以 PHP 形式表示为 $_GET['time']
。我建议转换为 DateTime
对象,以便您可以 format根据您的需要。
$time = new DateTime($_GET['time']);
如果您想使用 $_POST
,则必须更改为使用 $.post
。
$.post('remote.php', {
time: actualTime.toJSON()
}, function(json) {
var messages = json;
}, 'json');
关于javascript - 如何将参数发送到 php 脚本(使用 ajax)以便将它们包含在服务器端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046307/