segment.io javascript 发布失败并出现 CORS 错误

Posted

技术标签:

【中文标题】segment.io javascript 发布失败并出现 CORS 错误【英文标题】:segment.io javascript publish fails with a CORS error 【发布时间】:2014-09-26 03:05:54 【问题描述】:

当我尝试使用来自 segment.io 网站的示例“入门”javascript 代码加载网页时,我遇到了跨源资源共享 (CORS) 错误。下面是html本身。我没有在出售此页面的 play 2.1.3 服务器上设置任何标题,但我认为这不是必需的,因为其他用户向我保证他们运行此页面没有任何问题。:

<html>
<head>
<script type="text/javascript">
window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t)return function()var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics;for(var i=0;i<window.analytics.methods.length;i++)var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)window.analytics.load=function(t)if(!document.getElementById("analytics-js"))var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.io/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n),window.analytics.SNIPPET_VERSION="2.0.9",
window.analytics.load("**my_key_here**");
window.analytics.page();
</script>
</head>
<body>
</body>
</html>

当我使用浏览器加载此页面时,我在 javascript 控制台上看到以下错误:

[Error] Failed to load resource: Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin. (p, line 0)
[Error] XMLHttpRequest cannot load http://api.segment.io/v1/p. Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin. (localhost, line 0)

有什么想法吗?即使在 segment.io 网站上,也没有提到 CORS 应该只是使用 javascript 客户端发布事件的问题。

【问题讨论】:

【参考方案1】:

它可能是阻止跟踪脚本的浏览器扩展程序。你有安装一个吗?例如,我在 Chrome 上使用 Disconnect 并遇到了类似的错误,直到我将我的网站列入白名单。

我看到的错误是XMLHttpRequest cannot load http://api.segment.io/v1/i. The request was redirected to a URL ('about:blank') which has a disallowed scheme for cross-origin requests.

【讨论】:

以上是关于segment.io javascript 发布失败并出现 CORS 错误的主要内容,如果未能解决你的问题,请参考以下文章

javascript脚本事件,窗体失去焦点事件怎么写,怎么触发事件????

生存无法识别右删失数据

通过管道失败将结构写入孩子

Teradata 中易失性表的光标

javascript表单编程

OpenGL 纹理导致 ImGUI 窗口永久失焦