@delongeroman

Как сделать поочередный ajax запрос?

Добрый день.
В JQuery как и в JS я новичок, поэтому, вопрос самостоятельно решить не смог.
У меня есть селекты, при изменении отправляется запрос с параметром, ответ подставляется в нужное поле.
Как мне отправить 2 поочередных запроса, то есть, когда первый запрос придет и код появится в html, необходимо будет выполнить второй запрос, на основании тех данных, которые пришли от 1 запроса?
<select name="client" id="client" class="form-control mb-4">
            <option value="" id="remove">Сделай выбор</option> 
            {% for i in clientss %} <!-- цикл для вывода option -->
            <option value="{{i.company_id}}">{{i.client}}</option>
            {% endfor %} <!-- конец цикла -->
        </select>

        <label>Company</label>
        <div id="company" class="form-control">
       <!-- сюда приходит ответ в формате <option value="необходимый айди,
который вставляется в параметр запроса">Имя компании</option> -->
        </div>
        <br>

        <label>Group of Company</label>
        <div id="groupcomp" class="form-control">
       <!-- сюда должен прийти ответ от 2 запроса -->
        </div>


$("#client").change(function () {
      var url = $("#indexForm").attr("data-tgetc");  // get the url of the `yourpath` view
      var company_id = $(this).val();  // get the selected company ID from the HTML input
      $.ajax({                       // initialize an AJAX request
        url: url,                    // set the url of the request (= localhost:8000/path/)
        data: {
          'company_id': company_id       // add the programming id to the GET parameters
        },

        success: function (data) {  // `data` is the return of the `path` view function
          $("#company").html(data);  // replace the contents of the course input with the data that came from the server
          }
      });

Upd. Вопрос решен.
  • Вопрос задан
  • 243 просмотра
Решения вопроса 2
@Fenix957
$.ajax({ // initialize an AJAX request
url: url, // set the url of the request (= localhost:8000/path/)
data: {
'company_id': company_id // add the programming id to the GET parameters
},

success: function (data) { // `data` is the return of the `path` view function
$("#company").html(data); // replace the contents of the course input with the data that came from the server
$.ajax({ // initialize an AJAX request
url: url, // set the url of the request (= localhost:8000/path/)
data: {
'company_id': company_id // add the programming id to the GET parameters
},

success: function (data) { // `data` is the return of the `path` view function
$("#company2").html(data); // replace the contents of the course input with the data that came from the server
}
});
}
});
Ответ написан
@DaBags
Используйте параметр async: false
https://jquery-docs.ru/jQuery.ajax/
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы