php - 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'
我在各种帖子中看到,数据对象实际上可以通过删除请求发送。
我的代码可能有什么问题?
解决方案
使用 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
推荐阅读
- php - 有多少 PHP 会话可以访问/写入文件/数据库?(+最佳实践)
- firebase - 允许对 Firebase Firestore 中的一个特定集合具有读取权限
- python - 使用视图在 Django 中填充列表
- iterator - Iterating over symbols (assets) in TradeView (Pine @version=4)
- ruby-on-rails - Rails 6 默认绑定到 127.0.0.1?
- sql - 要插入的 countif 的 SQL 语法
- ruby-on-rails - Rails 复杂的活动记录关系
- assembly - 了解炸弹实验室第 5 阶段(两个整数输入)
- php - PHP相关查询
- javascript - 多次使用垂直标签切换