简体   繁体   中英

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?

<?
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. 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. Unless there's another way? Thank you!

$.getJSON('temp.php', {
    "test": 1,
    "data": JSON.stringify([])
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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