Advertisement
Guest User

Untitled

a guest
Jun 21st, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $('.filter_link').on('click', async function(event) {
  2.     let url = window.location.pathname;
  3.     window.location.pathname = '/4gb-v/';
  4.     event.preventDefault();
  5.     const gallery_products_display_div = $('.product-list');
  6.     let filter_category =  await fetch(url).then(function(response){
  7.         console.log('готово');
  8.     }).catch(function(err){
  9.         console.log('Fetch problem' + err.message);
  10.     });
  11.     // if (response.ok) { // если HTTP-статус в диапазоне 200-299
  12.  //  // получаем тело ответа (см. про этот метод ниже)
  13.  //  let json = await response.json();
  14.     // } else {
  15.     //   alert("Ошибка HTTP: " + response.status);
  16.     // };
  17.     let csrfToken = getCookie('csrftoken');
  18.     let data = {
  19.         'filter_category': filter_category,
  20.         'csrfmiddlewaretoken': csrfToken,
  21.     };
  22.     gallery_products_display_div.empty();
  23.  
  24.     $.ajax({
  25.         type: "POST",
  26.         data: data,
  27.         url: url,
  28.         success: function (response){
  29.  
  30.             //JSON.parse ожидает строку в параметре. Чтобы решить эту проблему, вам нужно привести в порядок свой JSON-объект.
  31.             document.querySelector('.product-list').textContent = response;
  32.             // let getData = JSON.parse(JSON.stringify(response));
  33.             // console.log(getData);
  34.         },
  35.         error: function (response){
  36.             alert('ошибочка вышла');
  37.         }
  38.     });
  39. });
  40.  
  41. // функция для получения куки
  42.  
  43. // let csrfToken = getCookie('csrftoken');
  44.  
  45. function getCookie(name) {
  46.     var cookieValue = null;
  47.     if (document.cookie && document.cookie !== '') {
  48.         var cookies = document.cookie.split(';');
  49.         for (var i = 0; i < cookies.length; i++) {
  50.             var cookie = cookies[i].trim();
  51.             // Does this cookie string begin with the name we want?
  52.             if (cookie.substring(0, name.length + 1) === (name + '=')) {
  53.                 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  54.                 break;
  55.             }
  56.         }
  57.     }
  58.     return cookieValue;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement