首页 > 解决方案 > nodejs中单击事件的Ajax调用问题

问题描述

我需要知道在 nodejs 中使用 ajax。我在我的 nodejs 应用程序中使用此代码

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {


    }
  };
  xhttp.open("POST", "/test1", true);
  xhttp.send();

问题是它在点击事件中只被调用了 6 次。第 7 次和第 8 次速度很慢,依此类推。它适用于页面刷新,但不适用于点击事件。

我的服务器端代码:-

router.post('/test1', function(req, res){
                console.log("TEST1");

});

标签: node.js

解决方案


尝试将某些内容作为响应返回给客户端:

res.send('');

或者,您可以使用next()将控制权发送回客户端:

router.post('/test1', function(req, res, next){
    console.log("TEST1"); next();
});

推荐阅读