Express.js – req.xhr 屬性
req.xhr 屬性是一個布林屬性,當請求的X-Requested-With 頭欄位是"XMLHttpRequest" 時,返回 True。True 指標基本上表示該請求是由客戶端庫(例如 jQuery)發出的。
語法
req.xhr
示例 1
以"reqXhr.js" 為名稱建立一個檔案,然後複製以下程式碼段。建立檔案後,使用命令"node reqXhr.js" 來執行該程式碼,如下面的示例所示 −
// req.xhr Property Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function (req, res) {
console.log("Xhr is: ", req.xhr);
res.send(req.xhr);
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用GET 請求訪問以下端點 − localhost:3000/api
輸出
C:\home
ode>> node reqXhr.js Server listening on PORT 3000 Xhr is: false
示例 2
我們來看另一個示例。
// req.xhr Property Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function (req, res) {
console.log("Xhr is: ", req.xhr);
res.send(req.xhr);
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用GET 請求訪問以下端點 −
https://:3000/api
並設定以下標頭檔案 −
X-Requested-With = XMLHttpRequest
輸出
C:\home
ode>> node reqXhr.js Server listening on PORT 3000 Xhr is: true
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP