【问题标题】:Using fetch with varible [duplicate]将 fetch 与变量一起使用 [重复]
【发布时间】:2020-10-03 10:54:10
【问题描述】:

我正在尝试从表单中获取信息并通过 fetch 将它们重定向到不同的服务器。

这是我的代码:

<html>
<body>

<h2> Login page </h2>

<form onsubmit="send()">

<p><label for="login">Login:</label><br />

<input type="text" id="login" name="login" size="20" autocomplete="off"></p>

<p><label for="password">Password:</label><br />

<input type="password" id="password" name="password" size="20" autocomplete="off"></p>

<button type="submit" name="form" value="submit">Login</bottun>
	</form>
</body>

<script>

function send(){

let USER=document.getElementById('login').value;

let PWD = document.getElementById('password').value;
		fetch('http://192.168.206.138:83/?username=${USER}&password=${PWD}');

	}
	</script>
</html>

问题在于获取。 它不转发参数 USER 和 PWD 的值 这是我从侦听此端口的服务器得到的: server side

【问题讨论】:

  • 单引号 ' 应替换为反引号 `。否则,您将无法在其中获得变量替换。
  • 这不是发送密码的“好习惯”。 1) HTTP 根本不安全; 2) 即使使用 HTTPS,密码在 URI 中也很简单。

标签: javascript php fetch let


【解决方案1】:

如果你想使用模板字符串文字,字符串应该带有反引号(`):

fetch(`http://192.168.206.138:83/?username=${USER}&password=${PWD}`);

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2017-02-01
    • 2021-03-21
    • 2019-03-14
    • 2017-11-25
    • 1970-01-01
    • 2020-10-09
    • 2020-06-17
    相关资源
    最近更新 更多