我试图在我发送请求的位置的html页面上显示响应,即,当前页面本身在这里我试图实现ajax请求,但是我对ajax还是陌生的,而且在我的前端代码中我也提到过使用post方法的动作标签,方法与在Ajax部分中捕获get响应的方式相同, 这里的问题是我应该在Ajax部分中提到URL,总之就是在同一html页面上实现发送请求和获取响应的正确方法。...
预先感谢。...
注意:我指的是此链接:https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_first 了解如何在当前页面本身上显示响应
前端代码:
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "URL", true);
xhttp.send();
}
</script>
</head>
<body>
<div align="center">
<form action='/Dasboard/SearchFriends' method="POST" onsubmit="return myFunction()">
<input tyoe="text" id="searchid" name="searchid" placeholder="Search Twitter">
<button type="submit" onclick="loadDoc()">submit</button>
</form>
<p id="demo"></p>
</div>
</body>
服务器端代码:
app.post('/Dasboard/SearchFriends' , function(req,res){
console.log(req.body);
console.log(req.body.searchid);
console.log(req.body);
const sql = "select * from users_info where Name LIKE '%"+req.body.searchid+"%' ";
mysqlConnection.query(sql,function(err,result){
const arr = result ;
console.log(app)
console.log(arr.length)
let countRecords = arr.length;
if (countRecords>0){
const names = arr.map(({ Name }) => Name);
console.log(names.length)
res.send(names)
// res.render('Dasboard-searh', {users:names});
} else {
console.log('Request failed...')
}
});
});