简体   繁体   中英

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

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 .

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.

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);
      }
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM