Ich kann einfach nicht die getCategory
Funktion erhalten, um alles außer undefined
oder false
zurückzugeben. Ich bin in der Konsole durchgetreten und alle Daten sind da. Ich bin auf dem besten Weg, immer noch unsicher Funktionen syncronoslyWie kann ich diesen Ajax-Aufruf nicht asynchron machen?
function getCategory(url) {
if (url) {
let message = false
$.ajax({
url: url,
global: false,
type: 'GET',
dataType: 'json',
}).done(function(data) {
message = `<a class="post__category post__link" href="${data[0].link}">${data[0].name}</a>`
return message
})
} else {
return ''
}
}
function posts() {
$.get(WPPosts, data => {
data.forEach(d => {
const excerpt = d.excerpt.rendered.substr(0, characterCount)
const featuredImage = d._links['wp:featuredmedia']
const featuredImageURL = featuredImage ? featuredImage[0].href : ''
const terms = d._links['wp:term']
const category = terms.filter(term => term.taxonomy === 'category')
const categoryURL = category[0].href
let post = `<article class="column">
<div class="decoration decoration__paper decoration__tape decoration__tape--left text-center post">
<a href="${d.link}">
${ getFeaturedImage(featuredImageURL) }
</a>
<h2 class="post__title">${d.title.rendered}</h2>
<p>
${d.date_gmt}
${ getCategory(categoryURL) }
</p>
<div class="post__excerpt">
${excerpt}... <a class="post__link" href="${d.link}">Read more</a>
</div>
</div>
</article>`
post = $.parseHTML(post)
postsWrapper.append(post)
});
})
}
ich die { async: false }
Option
Sie benötigen einen Rückruf in den 'getCategory' Funktion. Ajax ist definitionsgemäß ein asynchroner Aufruf (Akronym für "asynchrones JavaScript und XML"), Sie können es nicht synchron machen. –
Nun, ja, aber: http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax –
Stapal: Ich habe das versucht, aber es wurde für eine Weile bereinigt –