programlearned
  • Introduction
  • js 筆記
    • Hammer.js 應用
  • node 筆記
    • 安裝與開始
    • exports 的應用
    • http 模組知識
    • Express 開始
      • 載入靜態檔案(image,css,js)
      • ejs 的使用
      • ejs layout的使用
      • ejs include的使用
      • ejs-form 的使用
      • ejs-ajax 的使用
      • route 的整理使用
    • 參考資料
  • vue 筆記
    • 安裝與開始
    • 基礎知識
      • 創建vue與輸出
      • v-bind 綁訂應用
      • v-for 與 v-if
      • v-on
      • v-class
      • 計算功能函數
      • 表單資料綁定
      • TodoList製作
    • 參考資料
  • Bootstrap 筆記
    • 相關資源網站
    • 安裝與開始
    • CSS 基礎知識
    • 容器container 應用
    • 網格系統 應用
      • 斷點
      • 版本斷點比較
      • 欄位排序
      • 欄位推移
      • 巢狀欄位
      • Flexbox 方向性
    • 間隔工具
    • 按鈕(button)
    • 不同的呈現(display)方式
    • 容器對齊方式
    • 文字(背景顏色)對齊與樣式
    • 導覽列應用
    • 參考資源
  • gitbook 製作與離線版
Powered by GitBook
On this page

Was this helpful?

  1. node 筆記
  2. Express 開始

ejs-ajax 的使用

建立與使用

  • 在專案router js檔加入

  • ps : 記得加入 app.use(express.static('public'));

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');
})
  • 在 public 資料夾加入 js 資料夾

  • 建立all.js檔案

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);
    }
})
  • 執行 : node 你的.js 並到127.0.0.1:3000/search 輸入任意字串 即可在終端機cmd看到成果

Previousejs-form 的使用Nextroute 的整理使用

Last updated 6 years ago

Was this helpful?