这是我的代码,不起作用:
var sessionListJSON = GatherSessionsFromPage();
$.post('ajax.php', {
_ExportMissingAddressesToXLS : 1,
sessions : sessionListJSON // ARRAY
}, function(data) {
});
这是我捕获它的 php
尝试:
else if (array_key_exists('_ExportMissingAddressesToXLS', $_POST)) { // {{{
$sessions = $_POST['sessions'];
ExportMissingParentAddressesToXLS($sessions);
} // }}}
如何通过 jQuery
.post
正确发送该数组,然后在 PHP 中检索它?另外,这会是序列化的情况吗?如果是这样,你如何为 JS/jQuery 做到这一点?
最佳答案
jQuery 在 $.post()
中自动为您序列化数组。在 PHP 中,如果 sessionListJSON
是一个数组,则 $_POST['sessions']
将是一个数组。您可以通过普通数组索引、foreach
等来访问它。
如果sessionListJSON
确实是一个JSON字符串,则需要在PHP中对其进行解码:
$session = json_decode($_POST['sessions']);
关于javascript - 如何在 jQuery 中通过 .post 发送数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19870532/