首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的JavaScript应用程序接口调用返回400错误?

为什么我的JavaScript应用程序接口调用返回400错误?
EN

Stack Overflow用户
提问于 2018-11-16 06:31:31
回答 1查看 242关注 0票数 1

我有一个API调用,它接收到"Failed to load Resource“400错误。我已经尝试删除cookie和缓存,并使用不同的浏览器。当我看到图像大小可能是问题时,我也删除了一个图像。我读到了对HTTP头的需求,但不知道它将在我的代码中放在哪里,或者它看起来会是什么样子。任何帮助都将不胜感激!这是我的JS:

代码语言:javascript
复制
    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();
EN

回答 1

Stack Overflow用户

发布于 2018-11-16 06:46:09

如果您尝试在浏览器中加载https://www.themuse.com/api/public/jobs,则会出现一个400错误以及以下错误消息:

{“代码”:400,“错误”:“不是‘页面’的有效参数”}

该页面要求您通过GET请求为其提供一个名为page的非空变量。

如果您将页面加载为https://www.themuse.com/api/public/jobs?page=1 (例如),则可以成功加载数据,您的问题就会消失!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53328794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档