从源'null'对XMLHttpRequest的访问已被CORS策略阻止:

时间:2018-12-19 02:33:19

标签: javascript node.js xmlhttprequest http-headers

我正在根据从heremaps api获取的数据向darksky api请求数据,特别是我正在尝试获取某个地方的地理坐标并从darksky获取天气。

const getWeather = (longi, latit) => {
return new Promise((resolve, reject) => {
console.log("ia m in hello world");

const wetherHttpRequest = new XMLHttpRequest();

wetherHttpRequest.addEventListener("readystatechange", e => {
  //console.log(e.target);

  if (e.target.readyState === 4 && e.target.status === 200) {
    const data = JSON.parse(e.target.responseText);
    console.log("data resolved");
    resolve(data);
  } else if (e.target.readyState === 4) {
    console.log("data rejected");
    reject("error occured in getting weather");
  }
});

wetherHttpRequest.open(
  "GET",
  `https://api.darksky.net/forecast/8f03e2f020819a37720/${longi},${latit}`
);

wetherHttpRequest.setRequestHeader("Access-Control-Allow-Methods", "GET");
wetherHttpRequest.setRequestHeader("Access-Control-Allow-Headers", "*");

wetherHttpRequest.send();

});
};

在这里,两个论点longi和latit是地理代言人诺言的重犯诺言

getGeoLoc().then(
a => {
  const datain = a.Response.View[0].Result[0].Location.DisplayPosition;

  console.log(datain.Latitude);
  console.log(datain.Longitude);

  document.getElementById("out1").innerHTML = datain.Longitude;
  document.querySelector(".out2").innerHTML = datain.Latitude;

  getWeather(datain.Longitude, datain.Latitude).then(
    a => {
      console.log(a);
    },
    b => {
      console.log(b);
    }
  );
}

);

我正在

Access to XMLHttpRequest at 'https://api.darksky.net/forecast/8f03e2f020b9a37720/79.84556,6.93195' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

0 个答案:

没有答案