Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

javascript - 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. 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).

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...