在 AJAX/JQuery 中以数组形式组合的数据发送


Sending data combined in a form as an array in AJAX/JQuery

我需要一些帮助来发送数据。

首先,我工作的脚本被用作Nagios的Mass Acknoledgments工具,灵感来自NagiosXI的可购买组件。我上次开发了它,数据处理/发送完全在 PHP 中。实际上,我重新编写了我的脚本,为此,我需要使用 AJAX。

我遇到的问题是这样的:下面是我的表单屏幕,生成它的html代码,Chrome调试工具的屏幕,向您展示我的数据是如何发送的,以及用于数据检索的PHP代码。

大规模工具(Mass Acknoledgments Tools

    <tr>
   <td class="OK">168VWL1</td>
   <td><a href="javascript:checkAll('host1');">Check all for this hosts</a></td>
   <td class="empty"></td>
   <td class="empty"></td>
   <td class="empty"></td>
   <td class="empty"></td>
</tr>
<tr>
   <td class="empty"></td>
   <td class="critical"><input class="host1 servicecheck" type="checkbox" name="services[]" value="168VWL1::Explorer">Explorer</td>
   <td class="output">Explorer.exe: not running</td>
   <td class="centerd"><input type="checkbox" class="sticky" name="sticky[]" value="168VWL1::Explorer"></td>
   <td class="centerd"><input type="checkbox" class="notify" name="notify[]" value="168VWL1::Explorer"></td>
   <td class="centerd"><input type="checkbox" class="persist" name="persist[]" value="168VWL1::Explorer"></td>
</tr>

数据发送

if(isset($_POST['hosts'])){
        $allHosts = json_decode($_POST['hosts']);}
    if(isset($_POST['services'])){
        $allServices = json_decode($_POST['services']);}
    if(isset($_POST['sticky'])){
        $allStickys = json_decode($_POST['sticky']);}
    if(isset($_POST['notify'])){
        $allNotifys = json_decode($_POST['notify']);}
    if(isset($_POST['persistent'])){
        $allPersistents = json_decode($_POST['persistent']);}

如您所见,通过这样做,在 PHP 中处理我的数据非常简单,只需调用一次,我的数组就已经生成了。

现在,因为我使用 AJAX,我不知道如何以相同的方式发送它。有什么建议吗?

序列化和发布表单数据 -

var formData = $('.campaign-form').serialize();
$.ajax({
    type: 'post',
    url: POST_URL_HERE,
    data: formData,
    success: function(data)
    {
        console.log(data);
    }
});

$('#form1').serialize()

使用 AJAX 发送表单数据