Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从源站访问XMLHttpRequest';空';已被CORS策略阻止:_Javascript_Node.js_Xmlhttprequest_Http Headers - Fatal编程技术网

Javascript 从源站访问XMLHttpRequest';空';已被CORS策略阻止:

Javascript 从源站访问XMLHttpRequest';空';已被CORS策略阻止:,javascript,node.js,xmlhttprequest,http-headers,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(); wetherHtt

我正在请求一个基于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.