Jacob Lowe
Jacob Lowe

Reputation: 688

passing a variable to a ajax request Jquery

I am trying to pass a variable to an ajax request via jquery. I have a triggered event that defines the variable. I want to do this so that I can have one ajax request handle multiple urls. I have alerted the url and it come back fine, but when I plug it into the URL slot of a $.ajax request my targeted content does not load. Here is my code.

$(document).ready(function(){

   $('a').live('click', function(){
      var link = $(this).attr("href");
       $.ajax({
           url: link,
           success: function(html){
                $('#load').html(html)
           }
      });
   });

});

this has been a real headache and I feel I am just misusing something. Please help.

Upvotes: 1

Views: 507

Answers (2)

Nick Craver
Nick Craver

Reputation: 630409

There is a shortcut for this using .load() :)

$('a').live('click', function(){
  $('#load').load($(this).attr("href"));
});

Be aware of the same origin policy that Justin is alluding to as well, requests to different domains are prohibited for security reasons.

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134157

What URL are you passing in? If it is for a different domain the AJAX call will not be executed.

Upvotes: 1

Related Questions