req.xhr在express中的应用

req.xhr判断请求来自ajax还是普通请求;

若为ajax则是为true

这个属性是通过判断headers中的

x-requested-with的值来判断的

下面是来自ajax的请求:

 1   host: 'localhost:3000',
2 connection: 'keep-alive',
3 accept: '*/*',
'if-none-match': 'W/"b5-nK3C+n/aPR++6ICHo9ZKmQ"',
5 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.8 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
referer: 'http://localhost:3000/',
8 'accept-encoding': 'gzip, deflate, sdch',
9 'accept-language': 'zh-CN,zh;q=0.8',
10 cookie: 'privatecloud_sid=s%3AXUaL1RJr1h2r6yb52djAwrK3TWaIL91V.iuM%2FJ5a0MgnJcBrBPBOqFVShhezZly%2BwxxZ2O2bIa%2Bg'

下面是普通求求:

 1   host: 'localhost:3000',
2 connection: 'keep-alive',
pragma: 'no-cache',
'cache-control': 'no-cache',
'upgrade-insecure-requests': '1',
6 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.8 Safari/537.36',
7 accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
8 'accept-encoding': 'gzip, deflate, sdch',
9 'accept-language': 'zh-CN,zh;q=0.8',
10 cookie: 'privatecloud_sid=s%3AXUaL1RJr1h2r6yb52djAwrK3TWaIL91V.iuM%2FJ5a0MgnJcBrBPBOqFVShhezZly%2BwxxZ2O2bIa%2Bg'

以上是通过req.headers打印出来的;

红色为相同属性,其他为不同属性;可以根据这些属性来做判断;

上一篇:vue.js+boostrap最佳实践


下一篇:江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园