首页 > 解决方案 > Axios DELETE 请求正文被发送为空

问题描述

我正在尝试向我的 PHP/Codeigniter api 发送删除请求。从 NativeScript-Vue 前端发送。

 async deleteBackedupImages(identifiers) {
        console.log(identifiers);
        try {            
            var { data } = await axios({
                url: this.apiUrl + "/images?XDEBUG_SESSION_START=dsadsad",
                method: "delete",
                data:{
                    identifiers
                },
                headers: { "X-Requested-With": "XMLHttpRequest","Content-Type": "application/json" }
            });
            return data;
        } catch (error) {
            throw error;
        }
    }

在 PHP 方面,我有这个函数来处理 JSON 数据:

function getJSONData():stdClass{
    try {
        $ci =& get_instance();
        $stream_clean = $ci->security->xss_clean($ci->input->raw_input_stream);
        $request = json_decode($stream_clean);
        return $request;
    } catch (\Throwable $th) {    
       throw $th;
    }

}

“标识符”只是一个字符串数组。

$stream_clean 变量显示为空字符串,而不是 JSON 字符串。

我不得不说这有点奇怪,Axios 文档声明如下:

//data是要作为请求正文发送的数据 // 仅适用于请求方法 'PUT'、'POST' 和 'PATCH'

我在各种帖子中看到,数据对象实际上可以通过删除请求发送。

我的代码可能有什么问题?

标签: phpajaxaxios

解决方案


使用 params 而不是 body 通过 Axios 发送 DELETE 请求:

var { data } = await axios({
   url: this.apiUrl + "/images?XDEBUG_SESSION_START=dsadsad",
   method: "delete",
   params:{
      identifiers
   },
   headers: { "X-Requested-With": "XMLHttpRequest","Content-Type": "application/json" }
});

请注意,根据Axios 文档

// `params` are the URL parameters to be sent with the request
// Must be a plain object or a URLSearchParams object

推荐阅读