[英]How to send an empty array from JS to PHP using JSON?
Is it possible to send an empty array to PHP from JS using JSON? 是否可以使用JSON从JS向PHP发送空数组?
<?
if ($_GET['test']) {
$data = $_GET['data'];
print_r($data);
exit;
}
?>
<head>
<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
</head>
<script type="text/javascript">
$.getJSON('temp.php', {
"test": 1,
"data": []
})
</script>
This is a simplified version of what I'm trying to do. 这是我正在尝试做的简化版本。 Basically, I'm sending data to PHP so it can update the database.
基本上,我将数据发送到PHP,以便它可以更新数据库。 If I send an empty array, it should save an empty array.
如果我发送一个空数组,它应该保存一个空数组。 However, in the above example, only "test" gets passed and "data" gets thrown away.
但是,在上面的示例中,仅通过了“测试”,而丢弃了“数据”。 The only solution I can think of is to do something sloppy, like this:
我能想到的唯一解决方案是做一些草率的事情,像这样:
if (! isset($data = $_GET['data']))
$data = array();
So, I'm basically just making an empty array after JSON/JS throws it away. 因此,我基本上只是在JSON / JS将其丢弃之后制作一个空数组。 Unless there's another way?
除非有其他方法? Thank you!
谢谢!
$.getJSON('temp.php', {
"test": 1,
"data": JSON.stringify([])
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.