Jeremy
Jeremy

Reputation: 1952

I can't convert my ajax $.get to $.ajax

I try to convert an ajax function that I do not understand. Here the original function :

$.get(page, function (data) {
  $('.former-students__list').append(data.student
  $('#load-more').data('next-page', data.next_page);
})

I'd like to convert it to make it more readable (with $ .ajax), So I could clearly see the events success, and so on.

Here is what I tried but it doesn't work

$.ajax({
 url: page,
 data: function (data) {
  $('.former-students__list').append(data.students);
  $('#load-more').data('next-page', data.next_page);
 }
 'success': 'Cool, it's work')
},

Thank you for your help and your explanations

Upvotes: 1

Views: 86

Answers (3)

bik
bik

Reputation: 339

you should fire the success callback event so try this :

  $.ajax({
      url: page,  
      success: function(data) {
         $('.former-students__list').append(data.students);
         $('#load`enter code here`-more').data('next-page', data.next_page);
      }
   });

Upvotes: 1

JJWesterkamp
JJWesterkamp

Reputation: 7916

Try this:

$.ajax({
 url: page,
 success: function (data) {
  $('.former-students__list').append(data.students);
  $('#load-more').data('next-page', data.next_page);
 }
})

I think the data property is for POST request payloads.

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

The issue is because the data parameter should be an object, a string or an array which you use to send data to the server. However, given your $.get sample, you don't actually need to use it in $.ajax at all.

Instead, the success property should be a function which receives the data back from the request and then acts on it. Try this:

$.ajax({
  url: page,
  success: function(data) {
    $('.former-students__list').append(data.students);
    $('#load-more').data('next-page', data.next_page);
  })
});

For more information, please read the jQuery $.ajax documentation.

Upvotes: 2

Related Questions