【问题标题】:Google Cloud Platform Get Request from Tag Manager谷歌云平台从标签管理器获取请求
【发布时间】:2020-07-21 20:06:53
【问题描述】:

我希望将来自 Google 跟踪代码管理器的 HTTP 获取请求作为自定义 Javascipt 发送到 GCP 函数并让它返回一个值。我知道响应可能会很慢,但没关系。

我已经能够在 GCP 中创建该函数,当我从浏览器中仅使用 HTTP URL 调用它时,我得到了预期的响应。太好了,但是....当我使用 HTTP 请求变量调用时,我在 CORS 访问中被绊倒了。我已经使该功能可以公开访问,但这并不能解决我的问题。已添加 cors = require 但仍然没有运气。

任何帮助将不胜感激。

function reqListener () {
console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "https://us-central1-something.cloudfunctions.net/a_test?column=visitNumber&name=bigquery-public-data.google_analytics_sample.ga_sessions_20170801&visit=1501651467");
oReq.send();

【问题讨论】:

标签: javascript google-cloud-platform google-tag-manager


【解决方案1】:

您需要在函数响应上设置CORS头,头Access-Control-Allow-Origin帮助您允许所有域*或限制允许获取信息的来源(域)。

这是一个只能在域https://example.com中执行的函数示例

exports.corsEnabledFunctionAuth = (req, res) => {
  // Set CORS headers
  
  res.set('Access-Control-Allow-Origin', 'https://example.com');

  // if you want allow any domain plese use this line and comment the previous line
  // res.set('Access-Control-Allow-Origin', '*');

  // please put your current code
    res.send('Hello World!');
};


【讨论】:

    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多