Pick up the image of a Json

2

Good morning! I'm new to javascript and I'm trying to put the image of a JSON from the bing site and put it in my body but I'm not getting it returns the following error:

  

Failed to load access-control-allow-origin:    link :   Cross origin requests are only supported for protocol schemes: http,   date, chrome, chrome-extension, https. (anonymous) @ test.html: 30   test.html: 35 connection error

Is the first time I use api can you help me please? To get only the image I need to create a developer key? follow my code:

function getData(url) { 
    return new Promise(function(resolve, reject){ 
      const req = new XMLHttpRequest() 

      req.open('GET', url) 
      req.onload = function () {

        if (req.status === 200) { 
          resolve(req.response)
        } else {
          reject(req.status, req.statusText) 
        }
      }
      req.onerror = function () { 
        reject("erro de conexão")

      }

       req.send() 
     })
  }

  const catchImage = document.getElementById("body") 

  getData 

  const url = getData("Access-Control-Allow-Origin: https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=pt-BR").then(function(response)
  {
    const jsonResponse =JSON.parse(response.images.url)
    console.log(response)
    catchImage.innerHTML = ""
    for (const url of jsonResponse["url"]) {
        catchImage.innerHTML = catchImage.innerHTML + "<img src='" + images.url +  "' />"
      console.log(url)
    }
  }, function(error) { console.log(error)}) 
    
asked by anonymous 30.08.2018 / 16:36

1 answer

0

You can use one of the extensions for chrome (for development):

Allow-Control-Allow-Origin: *

CORS Toggle

These extensions disable chrome security with respect to communication from different hosts without the authorization definition in the header (as done by the header () function).

A of your example does not have external access ie your front-end that consumes the API must be on the same host (IP + port) of the API, but as api is not yours then it will not work.

    
08.11.2018 / 14:50