我有以下 HTML 元素,我需要将其值插入到数据库中。
<p>You scored :: </p><p id="txtScore1"></p>
<input type="text" placeholder="Your Name" id="name">
这是 W3schools 网站上建议的用于完成任务的示例 javascript 代码。
xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
xmlhttp.send();
所以我在我的 javascript 文件中尝试了以下几行:
xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true);
xmlhttp.send();
它不起作用,在 chrome 控制台中,错误显示为 Uncaught Syntax Error。意外标识符(重复两次)。
你能指出我正在做的错误吗?
最佳答案
这是一个语法错误。
xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true);
应该是
xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value) + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true);
// ↑
// Added parenthesis
关于javascript - 如何使用从 html 文件中获取的两个参数编写 AJAX GET 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817127/