简体   繁体   English

如何阻止客户端向服务器发送Ajax请求?

[英]How do I stop clients from sending ajax request to the server?

I have a web application in-which the client side sends an ajax request to a node.js server for an update request to check if there's any notifications for the clients user... This request happens every 30 seconds and even though i had performance tests on it my server side can't really take it. 我有一个Web应用程序,客户端在该Web应用程序中向anode.js服务器发送ajax请求以请求更新请求,以检查客户端用户是否有任何通知...即使我有性能,此请求也会每30秒发生一次我的服务器端无法对其进行测试。 I have already updated the interval time to once every 2 minutes and yet clients who didn't refresh still sends the requests every 30 minutes. 我已经将间隔时间更新为每2分钟一次,但是未刷新的客户端仍然每30分钟发送一次请求。 how can I make those clients take the update by demand and stop them form sending those requests? 如何让那些客户按需进行更新并阻止他们发送这些请求?

Thanks in advance, Itay 在此先感谢,伊泰

Edit: Just to make it I have no problem limiting it and I already have. 编辑:只是为了使我没有限制它的问题,我已经有了。 The problem is that the user doesn't refresh the page so the client-side code that includes that limit doesn't refresh on his browser. 问题在于用户不会刷新页面,因此包含该限制的客户端代码不会在其浏览器上刷新。 so Ajax vs web-sockets is not really my issue... 所以Ajax vs Web-socket并不是我的问题...

setinterval() function of javascript can be quite useful in your case. javascript的setinterval()函数在您的情况下可能非常有用。

You can control the frequency of a function execution by calling that function indirectly via setinterval() function that is configured via special interval parameter of your choice.function will then execute at that interval specified in milliseconds. 您可以通过通过选择的特殊间隔参数配置的setinterval()函数间接调用该函数来控制函数执行的频率,然后函数将以指定的间隔(以毫秒为单位)执行。

You can simply limit the ajax requests by invoking the function sending ajax requests indirectly via setinterval() function and limit the interval to 30 seconds. 您可以通过调用通过setinterval()函数间接发送ajax请求的函数来简单地限制ajax请求,并将间隔限制为30秒。

hope this will be helpfull for you!! 希望对您有帮助!!

please comment for more clarifications. 请发表评论以获得更多说明。

I think you should use web sockets in your NodeJS server to have a persistent but light connection with websites. 我认为您应该在NodeJS服务器中使用Web套接字,以与网站建立持久但轻巧的连接。

Check out Socket.IO, for example: http://socket.io 检出Socket.IO,例如: http ://socket.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM