简体   繁体   English

用于从api获取json数据的url格式

[英]url format for fetch to get json data from api

I am new to using apis and the various methods to get data. 我不熟悉使用api和各种方法来获取数据。 I have been following a couple of tutorials including this one on fetch method on or XMLHttpRequest method to get data and can do this just with the apis used in the tutorials. 我一直在关注一些教程,包括关于on的fetch方法或XMLHttpRequest方法的一些教程,以获取数据,并且只需使用教程中使用的api即可做到这一点。 I am hoping to use this api but am having trouble, I think with just the form of the url. 我希望使用此api,但遇到麻烦,我认为仅以url的形式即可。 I am getting this error: 我收到此错误:

“Access to fetch at ' http://collections.anmm.gov.au/collections ' from origin ' http://localhost:8888 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' “ “访问源' http:// localhost:8888 '从' http://collections.anmm.gov.au/collections '处获取的访问已被CORS政策阻止:否'Access-Control-Allow-Origin'”

I have tried using "https" to avoid the CORS error but then get this error: 我尝试使用“ https”来避免CORS错误,但随后出现此错误:

net::ERR_CONNECTION_REFUSED. 净:: ERR_CONNECTION_REFUSED。

I can return json if I past this url directly into the browser: http://collections.anmm.gov.au/collections/json . 如果我直接将此URL传递到浏览器中,则可以返回json: http : //collections.anmm.gov.au/collections/json

What I would like to know is do I just have a problem with the url I am trying or if perhaps there is an issue with the api itself that is preventing me accessing the data. 我想知道的是,我所尝试的网址是否有问题,或者api本身是否有问题阻止了我访问数据。

Thanks in advance for any pointers. 在此先感谢您提供任何指导。

This is my javascript code: 这是我的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)
});

For those looking for help, I found this answer helpful - No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API 对于那些寻求帮助的人,我发现此答案很有帮助- 尝试从REST API获取数据时,请求的资源上没有“ Access-Control-Allow-Origin”标头

I changed by code to the following and successfully returned the json I was expecting: 我通过代码更改为以下内容,并成功返回了我期望的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?"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM