Jump to content

Can’t send data using AJAX

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 post
Share on other sites

Note that this line does nothing:


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...