jQuery AJAX调用删除空数组

时间:2018-08-08 22:14:15

标签: jquery ajax api

我正在用jQuery写一个AJAX调用,该调用调用了以下两个函数:

_remove: function(){
    if (confirm('Are you certain you wish to permanantly remove item from this device?')){ 
    var response = $conn.ajax({
        url: "api_url",
        async: true,
        method: "POST",
        data: this._getPayload(),
        processData: false,
        contentType: false,
    })
    .then(response => {
        return response;
    });
    }
  }



  _getPayload: function(){
    var formData = new FormData();
    formData.append('i_host[id]', refId);
    formData.append("i_host[ipv4addrs][][del][]", "xx.x.xxx.xxx");
    return formData;
  }
});

我可以使用一组成功的参数在邮递员中拨打电话:

i_host[id] = refId
i_host[ipv4addrs][][del][] = xx.x.xxx.xxx

此外,参数的JSON对象如下所示:

"parameters": {
        "i_host": {
            "id": "refId",
            "ipv4addrs": [
                {
                    "del": [
                        "xx.x.xxx.xxx"
                    ]
                }
            ]
        }
    }

但是,当我进行jQuery调用来更新API时,似乎出现了这个问题,我的日志似乎接受了正确的参数集,然后给我一个错误的参数集(看起来ipv4addrs是被接受为对象数组,然后错误为单个对象)。似乎第一个空数组已从调用中删除。

任何有关两个功能的帮助都将非常有用。或者关于如何进行ajax调用的任何输入也将非常有帮助。日志如下:

[DEBU|#   28|2018-08-08 14:56:44] : {"target"=>"update", "object"=>"i_hosts", "controller"=>"api", "i_host"=>{"id"=>"refId", "ipv4addrs"=>[{"del"=>["xx.x.xxx.xxx"]}]}, "action"=>"autoproxy"}
[DEBU|#   28|2018-08-08 14:56:44] : /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
[DEBU|#   28|2018-08-08 14:56:44] : {"i_host"=>{"id"=>"refId", "ipv4addrs"=> [{"del"=>["xx.x.xxx.xxx"]}]}}
[DEBU|#   28|2018-08-08 14:56:44] : =========================================================================================
[DEBU|#   28|2018-08-08 14:56:45] : =========================================================================================
[DEBU|#   28|2018-08-08 14:56:45] : {"response":{"status":false,"message":"Please check the errors array for detailed information on why your request failed to complete!","errors":[{"messages":["The passed value does not meet the mininum required standards"],"parameters":{}}],"data":{},"parameters":{"i_host":{"id":"refId","ipv4addrs":{"del":["xx.x.xxx.xxx"]}}}}}
[DEBU|#   28|2018-08-08 14:56:45] : =========================================================================================

1 个答案:

答案 0 :(得分:0)

Ajax不支持空数组:(