JQuery on click event send POST request

Trying to post a POST request for a click event using jQuery with no luck. This is what I am using:

    <script type="text/javascript">
        $('#taxi_update').click(
            $.ajax({
                'type':'POST',
                'data':'id=17446&chru=0',
                'success':function() { ... },
                'error':function(){ ... },
                'url':'/url/',
                'cache':false
            })
        );
    </script>
    <a href="#" id="taxi_update">update</a>

      

Unfortunately it doesn't send a POST request.

Any suggestions what could be wrong with this?

+2


a source to share


1 answer


Since your script occurs before the element, it needs to be wrapped in an event document.ready

and the click handler itself will be a function, for example:

$(function() {
  $('#taxi_update').click(function() {
    $.ajax({
      type: 'POST',
      data: 'id=17446&chru=0',
      success: function() { ... },
      error: function(){ ... },
      url: '/url/',
      cache:false
    });
  });
});

      



Most of the time you will need the code inside document.ready

, because the elements it is looking for might not be available / ready, for example:

$(function() { });
//or...
$(namedFunction);
//or...
$(document).ready(function() { });

      

+16


a source







All Articles