简体   繁体   English

在 $.POST 上获取 ERR_EMPTY_RESPONSE

[英]Getting ERR_EMPTY_RESPONSE on $.POST

I have a simple social networking site with chat functionality.我有一个带有聊天功能的简单社交网站。 I have used $.post a lot in multiple pages.我在多个页面中经常使用$.post The code works well on all pages except message.php where the user message is posted and fetched multiple times with $.post (used to work well on local server).该代码在除 message.php 之外的所有页面上都运行良好,其中使用$.post多次发布和获取用户消息(用于在本地服务器上运行良好)。

When the messaging between users occur simulateously, the website stops to respond.当用户之间的消息模拟发生时,网站停止响应。 On reload, the server goes down and ERR_EMPTY_RESPONSE message is shown.重新加载时,服务器关闭并显示 ERR_EMPTY_RESPONSE 消息。 The website again comes into operation after a couple of minutes.几分钟后,该网站再次开始运行。 To what I learnt, this is happening on pages that use $.post frequently.据我所知,这发生在经常使用 $.post 的页面上。

To summarize the situation, I have created a live test page .为了总结这种情况,我创建了一个实时测试页面 An ERR_EMPTY_RESPONSE occurs when input is given continuously for some seconds. ERR_EMPTY_RESPONSE当输入连续给出几秒钟时发生。
The page contents:页面内容:

a.php一个.php

<script>
$(document).ready(function(e) {
$(".abc").keyup(function(){
    var a = $(this).val();
    $(".showoff").text("wait..");
    $.post('bbs.php',{a:a},function(abc){
        $(".showoff").html(abc);
    });
});});
</script>
<input type="textbox" class="abc">
<div class="showoff">Type to Change Me!</div>

bbs.php bbs.php

<?php
echo $_POST['a'];
?>

I am hitting my head hard on the wall for a week.我的头在墙上重重地撞了一个星期。 So, Please help me with this problem.所以,请帮我解决这个问题。 Thanks in Advance.提前致谢。 Sorry for my lame English as well.也很抱歉我的蹩脚英语。

As you appear to want an autocomplete type setup, use a timer.当您似乎想要自动完成类型设置时,请使用计时器。 Reset it on each keypress and after a delay send your post.在每次按键时重置它,并在延迟后发送您的帖子。 In this example it will send 3 seconds after the last keypress.在本例中,它将在最后一次按键后 3 秒发送。

$(document).ready(function(e) {
  var timer;
  $(".abc").keyup(function() {
    var $input= $(this);
    // Start timer
    clearTimeout(timer);
    // Start a new 3 second timer
    timer = setTimeout(function() {
        // Once the 
      var a = $input.val();
      $(".showoff").text("wait..");
      $.post('bbs.php', {
        a: a
      }, function(abc) {
        $(".showoff").html(abc);
      });
    }, 3000);
  });
});

JSFiddle: https://jsfiddle.net/TrueBlueAussie/Locpnk35/ JSFiddle: https ://jsfiddle.net/TrueBlueAussie/Locpnk35/

This will avoid overloading your server as no more than 1 request every 3 seconds can come from the same user.这将避免您的服务器过载,因为每 3 秒来自同一用户的请求不超过 1 个。 If the response is slower than 3 seconds you may also need to disable the key handler while an Ajax request is in progress.如果响应慢于 3 秒,您可能还需要在 Ajax 请求正在进行时禁用键处理程序。

Simplest answer would be you allow your server to be spammed to the point that it stops responding (yet still recieving new connections).最简单的答案是你允许你的服务器被垃圾邮件发送到它停止响应的程度(但仍然接收新连接)。 If the connections are not closed(resolved) in time you will also hit limitation of concurrent browser connections to domain (which I think is really happening - browser blocking you in making those request).如果连接没有及时关闭(解决),您还会遇到对域的并发浏览器连接的限制(我认为这确实发生了 - 浏览器阻止您发出这些请求)。

You either switch to sockets, or send text to server on set interval of time.您要么切换到套接字,要么在设定的时间间隔内向服务器发送文本。 Alternatively you dont allow next post, till previous is resolved.或者,您不允许下一篇文章,直到上一篇得到解决。

Instead of allowing your server to be spammed, you can remove the handler before the first post and set it back again when the post returns.您可以在第一次发帖之前删除处理程序,并在发帖返回时将其重新设置,而不是让您的服务器被发送垃圾邮件。

$(document).ready(function(e) {
    var $abc = $('.abc'); //good idea to cache those jQuery selectors!

    function abcPost() {
        $abc.off('keyup', abcPost)
        var a = $(this).val();
        $(".showoff").text("wait..");
        $.post('bbs.php', {
            a: a
        },
        function(abc) {
            $(".showoff").html(abc);
            $abc.on('keyup', abcPost)
        });
    }

    $abc.on('keyup', abcPost);

});

Ajax syncronous : Make the ajax call syncronous. Ajax 同步:使 ajax 调用同步。 This will stop its thread untill the response is back, easy to implement but comes with the downside that the user cannot type anymore untill request is solved这将停止其线程,直到响应返回,易于实现,但有一个缺点,即在请求解决之前用户无法再输入

$.ajax({
        url: 'bbs.php',
        data: {a:a},
        success: function(abc){
            $(".showoff").html(abc);
        },
        async: false
    });

Global variable check: make a global variable that checks the state of previous request and doesn't allow future ones until it is resolved:全局变量检查:创建一个全局变量来检查先前请求的状态,并且在解决之前不允许将来的请求:

var waiting=false;

$(".abc").keyup(function(){

    if(!waiting){
         waiting = true;
         // code
         $.post('bbs.php',{a:a},function(abc){
            $(".showoff").html(abc);
            waiting=false;
         });
    }
 });

This is good.这很好。

var waiting=false;

$(".abc").keyup(function(){

    if(!waiting){
         waiting = true;
         // code
         $.post('bbs.php',{a:a},function(abc){
            $(".showoff").html(abc);
            waiting=false;
         });
    }
 });

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

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