【发布时间】:2017-12-22 07:10:31
【问题描述】:
我设法在我的 console.log 上显示了该值,但是当我使用从 WordPress REST API 提取的值来更改 CSS 背景图像时,我得到了一个“未定义”值。我正在尝试创建静态 HTML,但在我的网站上显示博客特色图片。
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+')');
【问题讨论】:
标签: jquery json wordpress rest api