我正在尝试将 XMLHttpRequest 从客户端 js 发送到我的 Node 服务器。但什么也没发生。我对这些东西很陌生。这是我在 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();
};
}
这是我在 Node js 中的路线
app.get('/fcm', function(req, res) {
console.log('here');
res.end('hee');
});
最佳答案
您没有向您创建的端点发出请求,您正在请求路由:/
(该路由可能存在也可能不存在)。将请求更改为
xhttp.open("GET","http://localhost:3000/fcm", true);
它应该可以工作(假设您的网页和服务器在同一端口上运行,否则您可能会遇到 CORS 问题)。
关于javascript - XmlHttpRequest 到 Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42753309/