简体   繁体   English

我无法将我的Ajax $ .get转换为$ .ajax

[英]I can't convert my ajax $.get to $.ajax

I try to convert an ajax function that I do not understand. 我尝试转换一个我不理解的ajax函数。 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. 我想将其转换为更具可读性(使用$ .ajax),因此我可以清楚地看到事件成功,依此类推。

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. 问题是因为data参数应该是用于数据发送到服务器的对象,字符串或数组。 However, given your $.get sample, you don't actually need to use it in $.ajax at all. 但是,给定$.get示例,实际上根本不需要在$.ajax中使用它。

Instead, the success property should be a function which receives the data back from the request and then acts on it. 相反, success属性应该是一个函数,该函数从请求中接收数据,然后对其执行操作。 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 . 有关更多信息,请阅读jQuery $ .ajax文档

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. 我认为data属性用于POST请求有效负载。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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