0

I have a send_ajax function like this

function send_ajax (data, request_path) {
    function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    const csrftoken = getCookie('csrftoken');
    $.ajax({
        url: 'http://127.0.0.1:8000' + request_path,
        type: "POST",
        headers: {'X-CSRFToken': csrftoken},
        data: JSON.stringify(data),
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        statusCode: {
            500: function (response) {
                console.log('ОШИБКА СЕРВЕРА - код 500');
            }
        }
    })
         .done(function  (data) {
            if (data["basket_dialog_response"]) {
                productAdded_basket_dialog_show(data['basket_dialog_response'])
            }
            if (data['callback']){ 

            //________________________________________________________
            let some_data = data['something']
            //I need to send THIS variable to the function call location
            }
        })

In it, after sending an ajax request to the django server, the response comes in data, I write the desired variable to done and now I need to pass it back to the handler, from where I called this function to perform a check and remove one item from the list via $(this).remove .

It looks like this:

 $(`.counter_display`).on('change', function () {
                let item_id_in_cart = $(this).attr('name')
                let item_quantity = $(this).val()
                let data = {'item_id_in_cart': item_id_in_cart, "item_quantity": item_quantity, "key": "recalc"}
                send_ajax(data, {{ request.path }})
                let text = send_ajax/done/my some var
                and for example, based on this variable, I do a check and, if necessary, I do:
                  $(this).remove() //this
            }
        )

Just calls the function and passes the data to be sent there. So how do I get that variable in the onchange handler after calling the function?

  • Have you ever heard about [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)? You will need them here if you want to return some data from this method. Also, [this](https://stackoverflow.com/a/35135488/5767872) will be usefull for you. Basically you need to return a promise from this function, in the place you called it you need to use `.then(data => doSomethingWithData)`. – Sergey Sosunov Sep 15 '22 at 19:18
  • @SergeySosunov, I've never heard of a promise, thank you, I'll read about it now – Тв0р0бушек Sep 15 '22 at 19:23

0 Answers0