【问题标题】:Identifier containing slash character '/' into the URI在 URI 中包含斜杠字符“/”的标识符
【发布时间】:2016-10-11 10:18:50
【问题描述】:

我正在使用 ExpressmongodbAngular 创建一个应用程序,我在 MongoDB 中的一个文档的标识符为 _id = '20161007/COMPANY-00/CL/01-01'

我正在尝试使用标识符通过 REstful API 从 Angular 获取数据:

var _id = '20161007/COMPANY-00/CL/01-01';

this.$http.get('/api/datadays/' + _id)
      .then(response => {....}

但结果是:

angular.js:11881 获取 http://localhost:9000/api/datadays/20161007/COMPANY-00/CL/01-01404 (未找到)

有没有办法在标识符中使用斜杠来与 node/express 中的 restful API 一起工作?

谢谢

【问题讨论】:

    标签: angularjs node.js mongodb express restful-url


    【解决方案1】:

    你应该看看Encode URI ComponentDecode URI Component

    前端代码:

    var _id = encodeURIComponent('20161007/COMPANY-00/CL/01-01');
    
    this.$http.get('/api/datadays/' + _id)
          .then(response => {....}
    

    后端代码:

    app.get('/api/datadays/:id', function(req, res) {
      let id = decodeURIComponent(req.params.id)
      ...
    })
    

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      相关资源
      最近更新 更多