Stop sending ajax request without refreshing page

I created an ajax request that sends some data to a php file on a button click event. Now, after sending the data, I want to limit the ajax request not to send data again and again by clicking the button. It sends data only when the page is refreshed (this means that you send data once when the page loads). How can I stop such requests. My ajax code is as follows:

$(".button").click(function (e) {
    e.preventDefault();
    $.ajax({
        type: 'post',
        cache: false,
        url: 'my/ajaxrequest.php',
        data: {
            result: 'hi test data'
        },
        success: function (resp) {
            $("#result").html(resp);
        }
    });
});
+5
source share
4 answers

Just replace .click()with.one()

This will prevent reuse of a few clicks $.ajax().

$(".button").one('click', function(e) {
  e.preventDefault();
  $.ajax({ ... });
}

, $.ajax() :

$(function() {
    $.ajax({
        type:  'post', 
        cache:  false ,
        url:  'my/ajaxrequest.php',
        data:  { result : 'hi test data' },
        success: function(resp) {
            $("#result").html(resp);
        } 
    });
});
+9

ajax (.. 200 && , ? , success XHR: requestLog.sendStatus = true;, ajax, (requestLog.sendStatus = true;) e.preventDefault. ajax, requestLog.sendStatus = true success ajax.

var requestLog = {};
    requestLog.sendStatus = false;

$(".button").click(function(e) {
   e.preventDefault();
   if(requestLog.sendStatus) return;  
   /* requestLog.sendStatus = true; */
   $.ajax({
      type:  'post', 
      cache:  false ,
       url:  'my/ajaxrequest.php',
       data:  { result : 'hi test data' },
       success: function(resp) {
          $("#result").html(resp);
          requestLog.sendStatus = true;
      } 
     });
});

? :

(function() {
  $.ajax({
     type:  'post', 
     cache:  false ,
     url:  'my/ajaxrequest.php',
     data:  { result : 'hi test data' },
     success: function(resp) {
          $("#result").html(resp);
     } 
  });
}());
+1

click eventHandler Ajax.

$("#Btn").unbind("click");

$.off("click", "input:button", foo);

0

:

$(".button").click(function(e) {
      e.preventDefault();
$(".button").unbind('click'); //unbind the bound event after one click
    $.ajax({
        type:  'post', 
       cache:  false ,
        url:  'my/ajaxrequest.php',
        data:  { result : 'hi test data' },
        success: function(resp) {
      $("#result").html(resp);

        } 

      });

    });
0