最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xmlhttprequest - segment.io javascript publish fails with a CORS error - Stack Overflow

programmeradmin1浏览0评论

I'm encountering a cross origin resource sharing (CORS) error when I try to load a webpage with the sample "getting started" javascript code from the segment.io website. Below is the html itself. I haven't set any headers on my play 2.1.3 server that's vending this page, but I assume that's not necessary because other users have assured me they have run this with no problems.:

<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>

When I load this page with my browser, I see the following errors on the javascript console:

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

Any ideas? Even on the segment.io site, there's no mention that CORS should be an issue just to publish events with the javascript client.

I'm encountering a cross origin resource sharing (CORS) error when I try to load a webpage with the sample "getting started" javascript code from the segment.io website. Below is the html itself. I haven't set any headers on my play 2.1.3 server that's vending this page, but I assume that's not necessary because other users have assured me they have run this with no problems.:

<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>

When I load this page with my browser, I see the following errors on the javascript console:

[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)

Any ideas? Even on the segment.io site, there's no mention that CORS should be an issue just to publish events with the javascript client.

Share Improve this question asked Aug 3, 2014 at 13:34 pck.atlpck.atl 1332 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

It could be a browser extension that blocks tracking scripts. Do you have one installed? For example, I was using Disconnect on Chrome and was experiencing a similar error until I whitelisted my site.

The error I was seeing was 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.

发布评论

评论列表(0)

  1. 暂无评论