-1
내 console.log에 값을 표시 할 수 있었지만 WordPress REST API에서 추출한 값을 사용하여 CSS 배경 이미지를 변경하면 " 정의되지 않은 "값. 정적 HTML을 만들려고하지만 내 사이트에 블로그 추천 이미지를 표시하려고합니다.WordPress REST API (JSON)로 CSS 배경 이미지 변경
JS :
var newImage;
var postRequest = new XMLHttpRequest();
postRequest.open('GET','http://method.com.sg/post/wp-json/wp/v2/posts');
postRequest.onload = function(){
var ourData = JSON.parse(postRequest.responseText);
newImage = ourData[0].better_featured_image.source_url;
console.log(newImage); //Will output the hyperlink
};
alert(newImage); // Output Undefined
postRequest.send();
//Unable to work
$('.container.withBg.post').css('background-image', 'url('+newImage+')');
아! 거짓 기능으로 제 목숨을 구했습니다. @yamu Yamu 감사합니다. –