簡體   English   中英

使用Ajax和Node.js的簡單按鈕單擊示例?

[英]Simple button click example with Ajax and Node.js?

我是Ajax和Node.js + Express的新手。 在這一點上,我試圖弄清楚如何通過按鈕與前端和后端通信。

我在HTML頁面上有一個按鈕,我想用它從后端調用函數並將文本輸出到客戶端。

以下是我為了我需要而拼湊的內容,但我正在尋找一個如何做到這一點的例子。

這一切都發生在/ page上

index.hjs文件

<button class="btn btn-success" onclick="install()">Install</button>

// Client Side Ajax Script
<script>
    $('button').click(function () {
        $.post('/page', {data: 'blah'}, function (data) {
        console.log(data);
      });
    }, 'json');
</script>

app.js文件

app.post('/page', function (req, res, next) {
  calling.aFunction();
  res.write('A message!');
});

這些是我需要的所有部件以及需要編輯哪些部件才能使此功能正常工作?

index.js

<button class="btn btn-success">Install</button>

// Client Side Ajax Script
<script>
    $('button').click(function () {
        $.post('/page', {data: 'blah'}, function (data) {
        console.log(data);
      });
    }, 'json');
</script>

app.js

app.post('/page', function (req, res) {
    calling.aFunction();
    res.send('A message!');
});

你應該看到“一條消息!” 在瀏覽器控制台中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM