这是到目前为止我要提出的内容:在console.log中,我尝试打印的内容没有得到任何信息。虽然也没有错误。我的目标是使用香草javascript(ES6)处理ajax请求(稍后再处理)。
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'https://www.website.com/wp-json/acf/v3/options/options', true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
loadJSON(function(response) {
// Parse JSON string into object
var actual_JSON = JSON.parse(response);
console.log(response)
});
}
});
这是我的网站
../options...
文件的外观:{"acf":{"1yr_short_copy":"<p>Our 1 Year Money Back Guarantee either leaves you 100% satisfied,....
因此,例如-我只想获取字段
1yr_short_copy
文本数据并打印到html div中。我知道使用jQuery非常简单。但我无法在当前应用程序上使用jQuery-因此正在寻求Vanilla ES6技术。 最佳答案
您可能要使用onsuccess
方法。它是XMLHttpRequest类的另一个原型,该类在响应转变后即可工作。
const text = document.querySelector("#paragraph")
xobj.onsuccess = () => {
const response = JSON.parse(xobj.responseText);
text.textContent = response.acf.1yr_short_copy;
}