ciao!
ho un problema con il recuperare i valori da una chiamata ajax, fatta da un'altra chiamata ajax.
mi spiego, questa è la chiamata ajax principale:
codice:
function getPostsNews(categoria) {
$.ajax({
url: '',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function (res) {
res.forEach(function (el) {
var img = '';
if (typeof el["post-meta-fields"]["_thumbnail_id"] !== "undefined") {
img = getMediaId(el["post-meta-fields"]["_thumbnail_id"], false);
console.log(img);
} else {
img = 'images/white.jpg';
}
});
}).fail(function (err) {
});
}
li richiamo la funzione getMediaId che deve effettuare un'altra chiamata ajax e restituire un valore.
io gli passo async false, in quanto non deve essere asincrona.
faccio così:
codice:
function getMediaId(id, async = true) {
$.ajax({
url: '',
type: 'GET',
aysnc: async,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
return res.source_url;
},
error: function () {
return 'images/white.jpg';
}
});
}
il valore di source_url è corretto.
però alla prima chiamata arriva sempre undefined.
per quale motivo???