首页 > 解决方案 > 前端的 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) {

标签: javascript

解决方案


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 中的路径组件或作为查询字符串参数的内容。

看:


推荐阅读