ejs-ajax 的使用
建立與使用
app.post('/searchAJAX',function(req,res){
console.log(req.body);
console.log(req.body.list[2]);
console.log('hello!!')
// console.log(req.body);
// //轉址
res.redirect('search');
// res.render('search');
})var send = document.getElementById('send');
var content = document.getElementById('content');
send.addEventListener('click',function(e){
//避免submit事件
e.preventDefault();
var str = content.value;
var data = JSON.stringify({"content":str,"list":[1,2,3]});
//使用ajax傳送資料
var xhr = new XMLHttpRequest();
xhr.open('post','/searchAJAX');
//回傳字串類型資料
xhr.setRequestHeader("Content-type",
"application/json");
//欲傳送資料
xhr.send(data);
xhr.onload = function(){
console.log(xhr.responseText);
}
})Last updated
Was this helpful?