Так что мне нужны два вызова ajax, чтобы получить все данные.Я использую jQuery
вызов ajax для достижения этой цели.Но тогда я немного запутался в порядке исполнения.Вот мой проблемный код:
$.ajax({
type: "GET",
url: "/api/data",
dataType: "json"
}).then(function (data) {
console.log("I am the first")//correct
}).then(function () {
//second ajax
$.ajax({
type: "GET",
url: "/api/lifecyclephase",
dataType: "json"
}).then(function (data) {
console.log("I am the second")//third
})
}).then(function () {
console.log("I am the third")//second
})
Вывод
I am the first
I am the third
I am the second
Не должен ли then
дождаться, пока секунда ajax
завершит свою работу, прежде чем продолжить?
Правильный:
$.ajax({
type: "GET",
url: "/api/data",
dataType: "json"
}).then(function (data) {
console.log("I am the first")
}).then(function () {
$.ajax({
type: "GET",
url: "/api/lifecyclephase",
dataType: "json"
}).then(function () {
console.log("I am the second")
}).then(function(){
console.log("I am the third")
})
})