8、jquery-ajax

8-1 get

  1. $.get(url,res=>{
  2. console.log(res)
  3. })
  4. $.get(url).then(res=>{
  5. console.log(res)
  6. })

8-2 $.ajax

  1. $.ajax({
  2. url,
  3. type:"get",
  4. data, // get传值问号后面的值,可以放在data属性里面
  5. dataType,
  6. success:res=>{
  7. console.log(res);
  8. }
  9. })
  1. var url ="http://192.168.4.18:3000/search"
  2. $.ajax({
  3. url,
  4. type:"get",
  5. data:{
  6. keywords:"你"
  7. },
  8. success:res=>{
  9. console.log(res);
  10. }
  11. })

8-3 下拉刷新

  1. http();
  2. $(window).scroll(function () {
  3. if (isReachBottom()) {
  4. http();
  5. }
  6. })
  7. function http() {
  8. var url = "http://192.168.4.18:8000/more";
  9. $.ajax({
  10. url,
  11. method:"get",
  12. success: res => {
  13. res.forEach(item => {
  14. var html = `
  15. <div class="item">
  16. <img src=${item.imageUrl} alt="">
  17. </div>
  18. `
  19. $("#app").append(html)
  20. })
  21. setTimeout(sortBox, 100)
  22. }
  23. })
  24. }
  25. function sortBox() {
  26. var ww = $(window).width();
  27. var box = $(".item").outerWidth();
  28. var num = Math.floor(ww / box);
  29. var arr = [];
  30. $(".item").each((index, value) => {
  31. if (index < num) {
  32. console.log(value)
  33. var height = $(value).outerHeight();
  34. console.log(height)
  35. arr.push(height)
  36. console.log(arr)
  37. } else {
  38. //3.将元素放置在最小高度的位置 同时将数组重置
  39. var minHeight = Math.min(...arr);
  40. var index = arr.indexOf(minHeight);
  41. var offsetLeft = $(".item").eq(index).offset().left;
  42. $(value).css({ position: "absolute", left: offsetLeft, top: minHeight });
  43. arr[index] = minHeight + $(value).outerHeight();
  44. }
  45. })
  46. }
  47. function isReachBottom() {
  48. var scrollTop = $(window).scrollTop();
  49. var avaliHeight = $(window).height();
  50. var dh = $(document).height();
  51. return (scrollTop + avaliHeight > dh - 200) ? true : false;
  52. }