Getting ERR_EMPTY_RESPONSE on $.POST

前端 未结 5 1921
野的像风
野的像风 2020-12-21 13:06

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

5条回答
  •  轮回少年
    2020-12-21 13:36

    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.

提交回复
热议问题