javascript - 前端的 encodingURIcomponent 发送到后端快递服务器
问题描述
我以前从未使用过 encodeURIcomponent ,并且由于某种原因,我正在阅读的文档没有帮助,而且我迄今为止尝试过的方法也没有效果。(还在学习)
我试图在搜索时向我的后端快递服务器发送一个 url,这将返回一个响应。谁能提供一些关于我将如何做到这一点的见解?
这是我的前端:
async function getSample() {
const res = await fetch('http://localhost:3000/lookup/https://google.com');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
我后端的相关部分:
steve.open(req.params.url).then(function(site) {
解决方案
async function getSample() {
const uri = encodeURIComponent('https://google.com')
const res = await fetch(`http://localhost:3000/lookup/${uri}`);
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
encodeURIComponent('https://google.com')
为您提供https%3A%2F%2Fgoogle.com
允许作为 URI 中的路径组件或作为查询字符串参数的内容。
看:
推荐阅读
- shell - 如果子句在 Unix 中没有按预期工作
- pandas - 如何对 groupby 对象应用减法
- python-3.x - pie 不允许负值
- kubernetes - 从同一 VPC 中的工作程序节点和其他 EC2 实例访问 EKS DNS
- python - 我有大量数据,我想实现以下操作但需要太多时间。我该如何优化它?
- nuget - Gitlab CI/CD yml 文件,用于构建、打包和部署 .net 标准类库作为 nuget 到 nexus 存储库
- python - 使用 Mock() 在 Python 中模拟嵌套对象
- arrays - Json 响应 - 访问数组中的对象
- mysql - 使用带有 sum 子句的左连接问题
- java - 如何在 switch 语句中重置我的值?