我正在尝试将XMLHttpRequest从客户端js发送到我的节点服务器。但什么都没发生。我对这些东西很新。这是我在javascript中的功能。
function sendTokenToServer(token) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// document.getElementById("demo").innerHTML = xhttp.responseText;
console.log(xhttp.responseText);
}
xhttp.open("GET","http://localhost:3000/", true);
xtthp.send();
};
}
这是我在节点js中的路线
app.get('/fcm', function(req, res) {
console.log('here');
res.end('hee');
});
答案 0 :(得分:1)
您没有向您创建的端点发出请求,您正在请求路由:/
(可能存在也可能不存在)。将请求更改为
xhttp.open("GET","http://localhost:3000/fcm", true);
它应该可以工作(假设您的网页和服务器在同一个端口上运行,否则您可能会遇到CORS问题)。