Detecting AJAX requests on NodeJS with Express

后端 未结 2 1345
孤城傲影
孤城傲影 2020-11-30 04:25

I\'m using NodeJS with Express. How can I tell the difference between an ordinary browser request and an AJAX request? I know I could check the request headers but does Node

2条回答
  •  执念已碎
    2020-11-30 05:06

    Most frameworks set the X-Requested-With header to XMLHttpRequest, for which Express has a test:

    app.get('/path', function(req, res) {
      var isAjaxRequest = req.xhr;
      ...
    });
    

提交回复
热议问题