jQuery Ajax post with dynamic url

Posted by morpheous on Stack Overflow See other posts from Stack Overflow or by morpheous
Published on 2010-04-24T00:55:50Z Indexed on 2010/04/24 1:03 UTC
Read the original article Hit count: 300

Filed under:

I want to send an AJAX POST, but I want the url that the data is sent to, to be derived from the id of the element that was clicked.

This is what I have so far: I have written a javascript function (get_my_url) that returns a url, depending on the element id that was passed to the function. The problem is, it is not clear how to bind the function (get_my_url) to the url param of the .ajax() method.

jQuery('.elem1').click(function(){
    jQuery.ajax({
      async: false,
      type: 'POST',
      url: get_my_url(jQuery(this).attr('id')),
      data: 'id1=1',
      dataType: 'json',
      success: function(data){ msg = data.msg },
      error: function(xrq, status, et){msg='Oops. An error occured!'}
     });

Does anyone know how to pass a function to the url param?

© Stack Overflow or respective owner

Related posts about jQuery