我正在使用新的浏览器功能(navigator.sendBeacon)将异步数据发送到node.js服务器.
但我无法在节点服务器上收到它.那么任何人都可以告诉我如何在节点服务器上接收sendBeacon发布的数据.
节点服务器代码是:
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');
});
Run Code Online (Sandbox Code Playgroud)
客户端代码
navigator.sendBeacon('http://localhost:3000/','{"a":9}')
Run Code Online (Sandbox Code Playgroud) 我有一个检测最大长度的功能.但问题是当最大长度达到Ctrl + A组合时不起作用.如何通过javascript检测Ctrl + A组合.
这是我的maxlength代码.
if (event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 ){
return true;
} else {
if((t.length)>=50) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我想找到这个JSON对象的长度,所以任何人告诉我如何获得JSON对象的长度...意味着我想知道这个json对象包含多少数据.
var ddData = [{ "01":"United States",
"02":"United Kingdom",
"03":"Aruba",
"04":"United Kingdom",
"05":"Aruba",
"06":"Bahrain",
"07":"United Kingdom",
"08":"Algeria",
"09":"Andorra",
"10":"American Samoa",
"11":"United States"
}]
Run Code Online (Sandbox Code Playgroud) 当我点击任何DOM元素时,我想检查其parent元素是否包含特定的类.我正在尝试这种方式,但无法实现这一目标.有谁知道怎么样?
document.onclick=function(e){
console.log(e.parentElement('.drop').id);
}
Run Code Online (Sandbox Code Playgroud)
请仅用JavaScript提供解决方案 - 我不能使用jQuery.