php - 使用 axios 将方法修补到 Codeigniter 3
问题描述
我在 vue 3 中遇到了问题 qith“补丁”方法
这是我的前端 vue js 脚本:
updateUser(){
var formData = app.toFormData(app.currentUser);
axios.patch('http://localhost/rest-server/api/user', formData).then(function(response){
app.currentUser ={};
if(!response.data.status){
app.errorMsg = response.data.message;
} else {
app.successMsg = response.data.message;
app.getUsers();
}
});
}
和我的后端codeigniter 3(顺便说一句,我从这里https://github.com/chriskacerguis/codeigniter-restserver使用休息服务器库 :
public function index_patch()
{
$id = $this->patch('id');
$data = [
'first_name' => $this->patch('first_name'),
'last_name' => $this->patch('last_name')
];
if ($this->user_model->updateUser($data, $id) > 0) {
$this->response([
'status' => true,
'message' => 'User updated successfully.'
], REST_Controller::HTTP_NO_CONTENT);
} else {
$this->response([
'status' => false,
'message' => 'Failed to update.'
], REST_Controller::HTTP_BAD_REQUEST);
}
}
但它不起作用,当我尝试回显 $id 时,它为空。
编辑,尝试打印$this->patch(),并得到以下结果:
Array
(
[------WebKitFormBoundary4IqCBGyVSGTaLv6P
Content-Disposition:_form-data;_name] => "id"
7
------WebKitFormBoundary4IqCBGyVSGTaLv6P
Content-Disposition: form-data; name="first_name"
Brilian
------WebKitFormBoundary4IqCBGyVSGTaLv6P
Content-Disposition: form-data; name="last_name"
Aldamaa
------WebKitFormBoundary4IqCBGyVSGTaLv6P
Content-Disposition: form-data; name="created_at"
2021-05-18 08:56:16
------WebKitFormBoundary4IqCBGyVSGTaLv6P
Content-Disposition: form-data; name="updated_at"
2021-05-18 08:57:34
------WebKitFormBoundary4IqCBGyVSGTaLv6P--
)
我该如何解决这个问题?
解决方案
当您使用此库中的补丁方法时,您必须发送 JSON 而不是表单数据作为请求的正文。因为这个库是为 REST 准备的,标准的通信形式是 JSON。
推荐阅读
- javascript - 使用正则表达式在两个字符后拆分字符串
- node.js - 为什么它没有显示数据表单 api 链接
- javascript - 如何在所需文件中使用新写入的值?
- hibernate - 如何用queryDSL处理mysql json列?
- python - Mac 终端的字符串参数识别双引号
- python - Sharepoint 使用 Python o365 api 下载最后修改的文件
- node.js - AWS Etheruem 节点 Websocket 连接保持关闭
- java - 从 oracle 12c 升级到 oracle 19c 导致查询性能下降
- python - 如何使用自定义颜色映射将灰度图像转换为 RGB?
- node.js - 如何检查对象是否已从 Glacier 恢复?