在$ .POST上获取ERR_EMPTY_RESPONSE [英] Getting ERR_EMPTY_RESPONSE on $.POST

查看:1075
本文介绍了在$ .POST上获取ERR_EMPTY_RESPONSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的社交网站,提供聊天功能。我在多个页面中使用了 $。post
该代码在除了message.php之外的所有页面上都可以正常工作,其中用户消息被发布并多次使用
$。post
(用于在本地服务器上工作良好)。

I have a simple social networking site with chat functionality. I have used $.post a lot in multiple pages. 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).

当用户之间的消息模拟模拟时,网站停止响应。在重新加载时,服务器关闭,并显示ERR_EMPTY_RESPONSE消息。网站再次投入使用几分钟后。

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. The website again comes into operation after a couple of minutes. To what I learnt, this is happening on pages that use $.post frequently.

为了总结情况,我创建了一个 现场测试页当连续输入几秒钟时,会发生 ERR_EMPTY_RESPONSE

页面内容:


a.php

To summarize the situation, I have created a live test page. An ERR_EMPTY_RESPONSE occurs when input is given continuously for some seconds.
The page contents:

a.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

<?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.

推荐答案

由于您似乎想要自动完成类型设置,请使用计时器。重置它在每个按键和延迟后发送您的帖子。在此示例中,它将在最后一次按键后3秒发送。

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.

$(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/

这样可避免您的伺服器每3秒不超过1个请求可以来自同一个用户。如果响应慢于3秒,您可能还需要在Ajax请求正在进行时禁用键处理程序。

This will avoid overloading your server as no more than 1 request every 3 seconds can come from the same user. If the response is slower than 3 seconds you may also need to disable the key handler while an Ajax request is in progress.

这篇关于在$ .POST上获取ERR_EMPTY_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆