[尝试从api获取数据以在页面上显示小部件。这是我的提取请求:
var protocolToUse = (location.protocol == "https:") ? "https://" : "http://";
// url to fetch data from
const url = `${protocolToUse}polls-api.predictinteractive.com/get/catalog`;
fetch(url)
// Transform data into JSON
.then((results) => results.json())
.then(function(data) {
// Get the results
let widgets = data;
// For each widget, store their category and group name
for (i=0; i < widgets.length; i++) {
var category = widgets[i]["category"];
var group = widgets[i]["group"];
// This is the iframe link
var src_script = `${protocolToUse}w1.predictinteractive.com/widgets/poll_widget/index.html?{"category":"${category}"}`;
// Create a unique variable name for each widget
var named = nameInstance();
// The function createWidget formats the details to display the iframe and its details
named = new createWidget(
category,
group,
src_script
);
// This function appends each widget to an array called allWidgets
appendToArray(named);
}
// Get the widget in the allWidgets array
var firstWidget = allWidgets.shift();
// // Append the widget to the page
widgetContainer.appendChild(firstWidget);
})
.catch(function(error) {
console.log(error);
});
未显示小部件iframe,我认为可能是因为我在控制台中收到此警告:
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://w1.predictinteractive.com/widgets/poll_widget/index.html?{%22category%22:%22election-2020%22} with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
我也在Homestead上。让我知道是否需要提供更多信息,我们将不胜感激。
我现在只是在玩api,我真的无法重新创建您的问题。
您能告诉我您使用哪种浏览器吗?在Firefox上,我确实收到了来自api的响应以及所需的数据。
let protocolToUse = (location.protocol == "https:") ? "https://" : "http://";
const url = `${protocolToUse}polls-api.predictinteractive.com/get/catalog`;
async function fetchWídget(url) {
const widgets = await fetch(url).then(res => res.json());
console.log(widgets)
for (i = 0; i < widgets.length; i++) {
let category = widgets[i]["category"];
let group = widgets[i]["group"];
let src_script = `${protocolToUse}w1.predictinteractive.com/widgets/poll_widget/index.html?{"category":"${category}"}`;
let named = nameInstance();
named = new createWidget(
category,
group,
src_script
);
appendToArray(named);
}
let firstWidget = allWidgets.shift();
widgetContainer.appendChild(firstWidget);
}
fetchWídget(url);