使用Express检测NodeJS上的AJAX请求 [英] Detecting AJAX requests on NodeJS with Express

查看:270
本文介绍了使用Express检测NodeJS上的AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Express的NodeJS。如何知道普通浏览器请求和AJAX请求之间的区别?我知道我可以检查请求头,但是Node / Exprsss会公开这些信息?

解决方案

大多数框架设置 X请求 - 使用头到 XMLHttpRequest ,其中Express有一个测试:

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


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/Exprsss expose this information?

解决方案

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;
  ...
});

这篇关于使用Express检测NodeJS上的AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆