[英]Server sending multiple responses after AJAX request - using Socket.IO and ExpressJS
I'm trying to build a real-time program where users can set a marker on a Google Map and others who are connected can get that same marker.我正在尝试构建一个实时程序,用户可以在其中在 Google 地图上设置标记,而其他已连接的人可以获得相同的标记。 Everything seems to work fine except that after a few minutes, the server side is submitting the data a second time.
除了几分钟后,服务器端再次提交数据外,一切似乎都正常。
To clarify: client sets a marker on the map, the marker is sent to the server, running Node JS with Express JS, in JSON format.澄清一下:客户端在地图上设置一个标记,该标记被发送到服务器,以 JSON 格式运行 Node JS 和 Express JS。 The server returns the data to all connected clients.
服务器将数据返回给所有连接的客户端。 Minutes later, the server sends the same data it received once more, causing a "ERR_EMPTY_RESPONSE" client-side on the last line of example "Client code".
几分钟后,服务器再次发送它接收到的相同数据,导致“客户端代码”示例的最后一行出现“ERR_EMPTY_RESPONSE”客户端。
Client code:客户端代码:
var data = new Array();
data.push({lat: Gmap.markers[0].lat, lng: Gmap.markers[0].lng});
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/marker", true);
xhttp.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
xhttp.send(JSON.stringify(data));
Server-side:服务器端:
var app = express():
app.post('/marker', function(req,res){
io.emit('marker', req.body);
})
Anyone have any idea of whats going on?任何人都知道发生了什么?
You need to send a response to the http request.您需要发送对 http 请求的响应。 If you don't, the browser will time it out and may attempt to retry.
如果不这样做,浏览器将超时并可能尝试重试。
var app = express():
app.post('/marker', function(req,res){
io.emit('marker', req.body);
res.send("ok"); // <== Send a response to the http request here
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.