首页 > 解决方案 > 通过 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:等。

标签: javascriptexpresscrud

解决方案


推荐阅读