tags:

views:

41

answers:

1

Trying to send POST request on click event using jQuery with no luck. Here is what I use:

    <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 any POST request.

Any suggestions what could be wrong with this?

+7  A: 

Since your script occurs before the element, it needs to be wrapped in a document.ready event and the click handler itself much be a function, like this:

$(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'll want your code inside a document.ready because the elements it's looking for might not be there/ready yet, like this:

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