简体   繁体   English

Node.js,Express,Ajax:函数仅触发六次

[英]Nodejs, express, Ajax: Function triggers only Six Times

using nodejs and express I'm trying to trigger a js function which contains a simple console log. 使用nodejs表示我正在尝试触发一个包含简单控制台日志的js函数。 Sadly the function only triggers 6 times and freezes for a while. 可悲的是,该功能仅触发6次并冻结了一段时间。 After a minute or two all the button clicks which where clicked during the "frozen time" triggers at once. 一两分钟后,所有的按钮单击都会立即触发,在“冻结时间”内单击该按钮。 It happens always after 6 times of pressing a button. 它总是在按下按钮6次后发生。

index.html -> button index.html->按钮
client side -> jquery function that triggers an Ajax post function 客户端->触发Ajax发布函数的jquery函数
server.js -> contains the express function which triggers the console log server.js->包含express函数,该函数触发控制台日志

index.html 的index.html

<input type="button" id="car" value="drive"/>

clientside.js clientside.js

$('#car').click(function(){

   $.ajax({
      type:'POST',
      url: url + '/drive'
   });

});

server.js server.js

var app = express();

app.post('/drive', function(req, res){

   console.log('Car starts driving');

});

What am I doing wrong? 我究竟做错了什么? Any tips how I can improve my code? 有什么技巧可以改善我的代码吗?

Thank You 谢谢

Your server needs to respond to the request. 您的服务器需要响应该请求。 Try updating your server to: 尝试将服务器更新为:

var app = express();

app.post('/drive', function(req, res){

   console.log('Car starts driving');
   res.sendStatus(200)

});

This will return 200 OK to all requests. 这将对所有请求返回200 OK。

Why does this happen after 6 requests? 为什么在6个请求后会发生这种情况? I'm guessing you're using Chrome or Firefox. 我猜您正在使用Chrome或Firefox。 Chrome and Firefox will only allow a maximum of 6 concurrent requests to a single server. Chrome和Firefox仅允许最多6个并发请求到单个服务器。 Once you reach 6, the browser will queue the remaining requests. 达到6后,浏览器将把其余请求排队。

The reason your seeing it fix itself after a while is due to the request timeout. 一段时间后看到它修复的原因是由于请求超时。 Once the request has timed out (because it has received no response), the browser will close the connection. 一旦请求超时(因为没有收到响应),浏览器将关闭连接。

Browser concurrent request per host limits - https://stackoverflow.com/a/985704/4774345 每个主机的浏览器并发请求限制-https: //stackoverflow.com/a/985704/4774345

Your server code should return statement: 您的服务器代码应返回语句:

app.post('/drive', function(req, res){
   console.log('Car starts driving');
   res.sendStatus(sendStatus)
});

sendStatus details sendStatus详细信息

Good practice is to disable the button after clicking, and then enable it after the response comes back: 好的做法是在单击后禁用该按钮,然后在响应返回后将其启用:

//Disable button //禁用按钮

$.ajax({
    url: url + '/drive'        
    type: 'POST',        
    data: {},
    success: function(data){
        //Enable button
    },
    error: function(data){
        //Enable button
    }
});
$('#car').click(function(){

   $.ajax({
      type:'POST',
      async : true,
      url: url + '/drive'
   });

You need to send it asynchronous. 您需要异步发送它。

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

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