javascript - 通过 fetch() 在 Express 路由中使用 CRUD 操作时如何处理特殊字符?
问题描述
我有一个基本的 CRUD 系统设置,但是当使用特殊字符时系统会失败。
例如,这是我的删除请求路线:
// DELETE OPERATIONS
router.route('/delete/:id').delete((req, res) => {
const title = req.params.id;
// console.log('DEBUG: aricles.js: server receiving title: ', title)
DBM.deleteArticle(title).then(() => {
console.log('DEBUG: article deleted: ', title);
}).catch((err)=>{
console.log('articles/delete error', err);
});
res.end();
});
我的条目的唯一 ID 是一个字符串,它是文章的标题。但是,如果字符串以?
一个特殊字符结尾,它会在到达服务器时消失。
客户端发送删除请求如下:
const options = {
headers: {'Content-Type': 'application/json'},
method: 'DELETE',
body: JSON.stringify(this.props.data)
};
this.debug && console.log('cleint sending title: ', title);
fetch("/articles/delete/" + title, options )
.then((response) => {
console.log('response', response);
})
.catch((error) => {
console.log('delete clicked error', error);
});
我想 URL 中的其他特殊字符也会导致问题,例如#
and:
等。
解决方案
推荐阅读
- python - 使用 @jit 时出现奇怪的 Numba 0.50 行为
- nix - Nix 如何从 users.extraUsers 引用用户名?
- typescript - 为什么使用 jasmine 对 Node typescript 项目进行 Karma 单元测试会显示包含依赖项的覆盖范围?
- python-3.x - 不断从 request.get() 获取损坏的 .pdf
- android - 处理完深层链接后如何返回浏览器?Android & iOS(反应原生)
- python - 在 matplotlib 中更改轴
- php - 为什么提交此表单会提交错误的 ID?
- r - 从tomtom网站提取图形数据
- c# - 名称...在当前上下文 VS 2017 到 VS 2019 中不存在
- javascript - Js用公共键分组项目