javascript - 作为 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
。
解决方案
使用encodeURIComponent
:
let url = 'http://localhost:3000/link/?items='
+ encodeURIComponent('[{"quantity":1,"_id":"00001","box":"item01","desc":"#description1"}]');
推荐阅读
- html - 在页面上定位元素
- php - Ajax删除Dropzone Laravel POST数据空结果上的链接
- machine-learning - 在 Apache PredictionIO 中运行线性回归模型
- ckeditor5 - 如何在CKEditor5的表格中插入图像?
- c# - Microsoft 依赖注入和逆变
- c# - 为什么 MessageBox.Show 有时会失败?
- postgresql - 在 RETURNS TABLE pgplsql 函数中添加字段
- google-cloud-vision - 使用 boundingPolys 索引产品
- java - 将 swt 列表与包含两种不同样式的布局相关联
- mongodb - Mongo通过嵌套文档数组搜索