猿问

将 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 的值这是我从侦听此端口的服务器获得的: 服务器端


扬帆大鱼
浏览 130回答 1
1回答

波斯汪

如果你想使用模板字符串文字,字符串应该带有反引号(`):fetch(`http://192.168.206.138:83/?username=${USER}&password=${PWD}`);
随时随地看视频慕课网APP
我要回答