我正在使用新的浏览器功能 (navigator.sendBeacon) 异步向 node.js 服务器发送数据。
但是我无法在 node 服务器上接收它。所以有没有人能告诉我如何在 node 服务器上接收由 sendBeacon 发送的数据。
node 服务器代码如下:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// set cross origin header to allow cross-origin request.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json());
app.post('/',function(req,res){
console.log('i got the request',req.body)
});
var server = app.listen(3000, function() {
console.log('Express is listening to http://localhost:3000');
});
客户端代码
navigator.sendBeacon('http://localhost:3000/','{"a":9}')