3

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?

1 Answer 1

17

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() { });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.