[英]How can I use XMLHttpRequest() to constantly send data to another page in my website?
I'm working in node.js and express.js and am trying to find the FASTEST way I can deal with this problem. 我正在使用node.js和express.js,并试图找到最快的方式来解决这个问题。
One page in my website is receiving location information in latitude var lat
and longitude var long
, and I want to constantly be sending that information to another page on my website. 我网站上的一个页面正在接收纬度var lat
和经度var long
位置信息,因此我想不断地将该信息发送到网站上的另一页面。 My code is very incomplete but any guidance is extremely helpful. 我的代码很不完整,但是任何指导都非常有用。 I'm currently getting the error http://localhost/driver/save_position Failed to load resource: net::ERR_CONNECTION_REFUSED
and later POST http://localhost/driver/save_position net::ERR_CONNECTION_REFUSED
in the browser console. 我当前收到错误http://localhost/driver/save_position Failed to load resource: net::ERR_CONNECTION_REFUSED
,随后在浏览器控制台中发布POST http://localhost/driver/save_position net::ERR_CONNECTION_REFUSED
。 My code is below. 我的代码如下。
This is code in a javascript file run on one webpage that I want to send the information to my other page so that it can constantly update. 这是在一个网页上运行的javascript文件中的代码,我希望将该信息发送到另一网页,以便可以不断更新。
function sendCoordinates(lat, lon){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://" + window.location.hostname + "/driver/save_position", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('lat=' + lat + '&lon=' + lon);
}
In a separate file for my routing I have 在单独的文件中,我有
router.post('/driver/save_position',function(req, res, next){
console.log(req)
})
I'm very much a beginner with all of these kinds of ideas so any advice, help, and solutions are extremely appreciated! 我是所有这些想法的初学者,因此非常感谢任何建议,帮助和解决方案! Thank you for any help! 感谢您的任何帮助!
One web page does not send data directly to another web page unless it's in the same browser. 除非在同一浏览器中,否则一个网页不会将数据直接发送到另一网页。 So since you're saying "another page on your website", I assume you're talking about making data appear in some other page for some other user. 因此,既然您说的是“网站上的另一个页面”,那么我假设您正在谈论使数据在其他用户的其他页面上显示。 If that's what you mean, then that just isn't a capability that browser Javascript has. 如果这就是您的意思,那么那不是Javascript浏览器所具有的功能。 Also, it's not really clear what you mean when you say "send to a web page" as that isn't really a destination. 另外,当您说“发送到网页”并不是真正的目的地时,您的意思还不清楚。
What you can do is to send a request to a server and you can ask that server to notify some other connected user/page. 您可以做的是向服务器发送请求,然后您可以要求该服务器通知其他已连接的用户/页面。 If that other user/page has a live webSocket connect to the server, then server can send a message to that user/page over the webSocket. 如果该其他用户/页面具有与服务器的实时webSocket连接,则服务器可以通过webSocket向该用户/页面发送消息。
If the other user/page does not have a live webSocket connection to the same server, then the only way it can get this information from the server is for it to request that information from the server with its own Ajax call. 如果另一个用户/页面没有到同一服务器的实时webSocket连接,则它可以从服务器获取此信息的唯一方法是让它使用自己的Ajax调用从服务器请求该信息。 The server would have to store that information so when someone asks for it, it could supply the info. 服务器必须存储该信息,以便当有人要求时,它可以提供该信息。
If you are asking how to send data from one window in the browser to another window in the same browser, then you can use postMessage()
to send data between two coooperating windows in the same browser. 如果要询问如何将数据从浏览器的一个窗口发送到同一浏览器的另一个窗口,则可以使用postMessage()
在同一浏览器的两个协作窗口之间发送数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.