在cors请求中,预检的响应无效(重定向)

时间:2016-04-08 16:07:03

标签: javascript cors preflight

我正在尝试向

提出请求

http://ofmpub.epa.gov/enviro/frs_rest_services.get_facilities?facility_name=scrap&state_abbr=VA&program_output=yes&output=JSON

但我正在努力解决他们的问题。完整的错误消息是:

XMLHttpRequest cannot load http://ofmpub.epa.gov/enviro/frs_rest_services.get_facilities?facility_name=scrap&state_abbr=VA&program_output=yes&output=JSON. Response for preflight is invalid (redirect)

这是我的代码的样子。我以为我用*设置了正确的标题,但我猜不是。这里有什么问题?

fetchData (url) {
    var that = this;
  return new Promise(function(resolve, reject) {

    var req = new XMLHttpRequest();

    req.open('GET', url, true);
    req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
    req.setRequestHeader('Access-Control-Allow-Origin', '*');
    req.setRequestHeader('Access-Control-Allow-Methods', 'GET');

    req.onload = function() {
      if (req.status == 200) {
      console.log(req.responseText);
      that.updateData(JSON.parse(req.responseText));
        resolve(req);
      }
      else {
        reject(Error(req.statusText));
      }
    };

    // Handle network errors
    req.onerror = function() {
      reject(Error("Network Error"));
    };

    // Make the request
    req.send();
  });
}

0 个答案:

没有答案