Jump to content

Can’t send data using AJAX


unplugged_web

Recommended Posts

I've got a link that is sent using AJAX but no results are being sent. The link is:

<a href="javascript:void(0);" data-flavour="Berry" class="nav_link Berry">Berry</a>
While the AJAX is:
<script type="text/javascript">  $('.nav_link').on('click', function(){  event.preventDefault();    var flavour = $(this).data('flavour');$.get('/filter/?search=' + $(flavour).val(),{flavour:flavour}).done(function(data){    if(!$('#container').hasClass('is-loaded')) {    $('#container').addClass('is-loaded');    $('#container').html(data);} else {    $('#container').append(data);}     });  }  );</script>
When I get the filter page is says the search value is undefined. If I go directly to the page with what should be sent via AJAX (filter/?search=Berry) then I get all of the relevant results
I don't get any errors at all either
Link to comment
Share on other sites

Note that this line does nothing:

event.preventDefault();

because event is not defined (except in old versions of Internet Explorer where there was a global variable called "event", but it didn't have a preventDefault() method.

 

You should set event in the function:

$('.nav_link').on('click', function(event){

Link to comment
Share on other sites

Note that this line does nothing:

That would actually be a fatal error, something like undefined doesn't have a method called preventDefault, or trying to call a method on an undefined object. That one is definitely fatal.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...