【发布时间】:2018-11-15 22:31:31
【问题描述】:
我有一个 API 调用收到“加载资源失败”400 错误。我尝试删除 cookie 和缓存并使用不同的浏览器。当我看到图像大小可能是问题时,我还删除了图像。我读到了对 HTTP 标头的需求,但不明白它在我的代码中的位置或它的外观。任何帮助将不胜感激!这是我的 JS:
const app = document.getElementById('root');
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://www.themuse.com/api/public/jobs', true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
data.forEach(job => {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const h1 = document.createElement('h1');
h1.textContent = job.title;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Darn, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
【问题讨论】:
-
错误是
Not a valid argument for 'page',API使用是否正确? -
themuse.com/developers/api/v2 在该端点的文档上说页码是必需的,您必须发送该页码以获得有效响应。
标签: javascript api http-status-code-400