2017-01-18 73 views
0

我正在創建一個網站,其中後端是具有express的node.js,而客戶端使用的是mithril。我們不能使用ajax,form和jquery。如何從mithril調用app.js中的代碼

我的問題是我們如何才能從mithril js文件進行通信來表達?讓我給你舉例:

app.post("/testFunction",function(req,res){ 
    //Pass result 
}); 

在祕銀:

m.render(document.body, [ 
    m('input[type=text]'), 
    m('button'), 
    m('span', 'show the result') 
]); 

在這裏我有一個輸入框和一個按鈕。當我點擊按鈕時,它應該在express中調用我的功能,保存數據並返回要顯示在span中的消息。

任何想法?

+3

你說你不能使用ajax和窗體,那麼你怎麼能發佈到服務器? – ciscoheat

回答

0

您需要使用m.request才能發佈到您的端點。查看這裏的文檔(http://mithril.js.org/request.html)。這是一個簡單的例子。

m.render(document.body, [ 
    m('input[type=text]#testInput'), 
    m('button', { 
     onclick: function() { 
      document.getElementById("testInput").value 
      m.request({ 
        method: "PUT", 
        url: "http://127.0.0.1:3000/testFunction", 
        data: { 
         inputValue: document.getElementById("testInput").value 
        } 
       }) 
       .then(function(response) { 
        console.log(response); 
       }) 
     } 
    }, "Send Request") 
]);