首页 > 解决方案 > 作为 req.query 传递时带有井号 (#) 的文本在后端消失

问题描述

我正在尝试传递一个带有井号标签的字符串,req.query以将其解析为 JSON。

http://localhost:3000/link/?items=[{"quantity":1,"_id":"00001","box":"item01","desc":"#description1"}]

但是当它到达后端(Node + Express)req.query时变成

{
  items: '[{"quantity":1,"_id":"00001","box":"item01","desc":"'
}

如您所见,带有主题标签的描述刚刚消失,因此触发了SyntaxError: Unexpected end of JSON input* when being parsed as JSON

标签: javascriptnode.jsexpress

解决方案


使用encodeURIComponent

let url = 'http://localhost:3000/link/?items=' 
  + encodeURIComponent('[{"quantity":1,"_id":"00001","box":"item01","desc":"#description1"}]');

推荐阅读