交叉域名问题。需要调用获取请求到firebase托管的angular2 app

时间:2018-06-01 08:05:53

标签: angular firebase-hosting

我正在尝试从firebase托管的angular 2应用中获取价值。当我们尝试它时显​​示错误

无法加载

https://url/?a1=123&a2=milla: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
index.hmtl:22 XHR finished loading: GET "https://url/?a1=123&a2=milla".
loadXMLDoc @ index.hmtl:22
onclick @ index.hmtl:8

这是我的firebase.json数据

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/timestamp",
        "function": "app"
      }
    ],
    "headers": [ {
      "source" : "index.html",
      "headers" : [ {
        "key" : "Access-Control-Allow-Origin",
        "value" : "*"
      } ]
    } ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  }
}

这是我的ajax代码,用于从firebase托管的angular2应用程序

请求数据
var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "https://url/?a2=123&a1=milla", true);
  xhttp.send();

0 个答案:

没有答案