简体   繁体   English

如何使用XMLHttpRequest()不断将数据发送到网站的另一个页面?

[英]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.

相关问题 如何使用XMLHttpRequest来获取页面标题并将其返回到我的页面? - How can I use XMLHttpRequest to grab the title of a page and return it to my page? 我如何将数据发送到另一个页面而不将其附加在url中? - how can i send the data to another page without appending it in url? 如何在我的网站中将此按钮重定向到网站上的另一个页面? - How can I make this button in my website redirect to another page on the website? 如何检查用户是来自我网站的不同页面还是来自其他网站? - How can I check if a user is coming from a different page of my website or from another website? 当用户单击链接转到另一个网站时,我该如何请求用户留下。 但是,如果他们要访问我的网站上的其他页面,则没有要求? - How can I request a user to stay, when they clicked a link to go another website. But no request if they want to go another page on my website? 我正在尝试将JSON格式的API数据获取到我的网站。 如何获取所需的信息并将其发送到html页面中的div? - I am trying to get API data in JSON format to my website. How can I take the required info and send it to a div in the html page? 打开另一页时,如何最小化网站上的侧边栏? - How can i minimize the side bar in my website while opening another page? 如何使用 XMLHttpRequest 发送 JSON 数据以及从 GET 或 POST 使用哪种方法更好? - How can an XMLHttpRequest be used to send JSON data and which method is better to use from GET or POST? 我正在尝试使用Ajax将数据发送到另一个页面 - I'm trying to use Ajax to send data to another page 如何使用应用于XMLHttpRequest的ajaxComplete - How can I use ajaxComplete applied to a XMLHttpRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM