【问题标题】:url format for fetch to get json data from api用于从 api 获取 json 数据的 url 格式
【发布时间】:2019-04-04 12:24:09
【问题描述】:

我不熟悉使用 api 和各种获取数据的方法。我一直在关注一些教程,包括 this one on fetch method on 或 XMLHttpRequest 方法来获取数据,并且可以仅使用教程中使用的 api 来做到这一点。我希望使用this api,但我遇到了麻烦,我认为只是网址的形式。我收到此错误:

“访问 'http://collections.anmm.gov.au/collections' 从 来源“http://localhost:8888”已被 CORS 策略阻止:否 “访问控制允许来源”“

我曾尝试使用“https”来避免 CORS 错误,但随后出现此错误:

net::ERR_CONNECTION_REFUSED。

如果我将此网址直接传递到浏览器中,我可以返回 json:http://collections.anmm.gov.au/collections/json

我想知道的是,我只是尝试使用的 url 有问题,还是 api 本身存在问题,阻止我访问数据。

提前感谢您的任何指点。

这是我的 javascript 代码:

function createNode(element){
  return document.createElement(element);
}

function addClass(cls, el){
  return el.classList.add("cls");

}

function append(parent, el){
  return parent.appendChild(el);
}

const div = document.getElementById('root');
div.setAttribute('class', 'container');

//const url = 'https://randomuser.me/api/?results=100';
//can return data from this url

const url ='http://collections.anmm.gov.au/collections' // returns data if pasted directly into the browser
//const url = 'http://collections.anmm.gov.au/collections/json' // this is the format suggested in the api documentation I think


fetch(url)
  .then((resp)=> resp.json())
  .then(function(data){

    //create and append the list to the ul
    let authors = data.results; // get results

    return collections.map(function(collection){

      let card = createNode('div'),
        //  img = createNode('img'),
          h1 = createNode('h1');

          card.setAttribute('class', 'card');

      img.src = collection.notes.value;
      h1.innerHTML = `${collection.notes.value} ${collection.notes.value}`;

      append(card, h1);
      append(div, card);

    })
  })

.catch(function(error){
  console.log(error)
});

【问题讨论】:

标签: javascript json api fetch


【解决方案1】:

对于那些寻求帮助的人,我发现这个答案很有帮助 -No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API

我通过代码更改为以下并成功返回了我期望的json:

const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = "http://collections.anmm.gov.au/collections/json"; // site that doesn’t send Access-Control-*
fetch(proxyurl + url) // https://cors-anywhere.herokuapp.com/https://example.com
.then(response => response.json())
.then(contents => console.log(contents))

.then(function(data){

  let authors = data.results;
console.log(authors);
  return authors.map(function(author){

    let card = createNode('div'),
      //  img = createNode('img'),
        h1 = createNode('h1');

        card.setAttribute('class', 'card');

  //  img.src = author.picture.medium;
    h1.innerHTML = `${author.name.first} ${author.name.last}`; 
    //append(card, img); // append all our elements
    append(card, h1);
    append(div, card);

  })
})


.catch(() => console.log("Can’t access " + url + " response. Blocked by browser?"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2020-07-13
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多