客户端上的encodeURIComponent和服务器上的encodeURIComponent

时间:2019-01-29 21:28:48

标签: javascript web url uri

有人可以批评我这种方法吗?

我想使用GET方法发送JSON对象,但是主体不可用,因此我在客户端中对JSON进行了编码,并在服务器中对其进行了解码,如下所示:

// on client
 let query = {
    subject: subject,
    sources: sources
  }
query = encodeURIComponent(JSON.stringify(query))
console.log(encoded);

// on server
const { query } = url.parse(req.url)
decodedQuery = decodeURIComponent(query)

它可以正常工作,但是人们使用多个库,例如querystring和query-string或手动构建查询字符串。

那为什么不做我在做什么呢?

致谢

0 个答案:

没有答案