简体   繁体   English

使用cdn避免使用神秘的javascript“脚本错误”

[英]Avoid cryptic javascript “script error” using cdn

I'm using a script that detects Javascript errors and reports them to my backend. 我正在使用一个检测Javascript错误的脚本并将它们报告给我的后端。 However I am getting cryptic "script error" messages, which is not very helpful for debugging. 但是我收到了神秘的“脚本错误”消息,这对调试没有多大帮助。

According to Cryptic "Script Error." Cryptic“脚本错误”。 reported in Javascript in Chrome and Firefox the reason is because the script that threw the error is served from a different origin than my site. 在Chrome和FirefoxJavascript中报告的原因是因为抛出错误的脚本来自与我的网站不同的来源。

Since I'm using a CDN all of my scripts are effectively served from another domain. 由于我正在使用CDN,因此我的所有脚本都可以从另一个域有效地提供。 Is there a way to get more useful error messages while still using a CDN? 有没有办法在使用CDN时获得更多有用的错误消息?

Also everything is served over SSL so I would like to retain this ability. 一切都通过SSL提供,所以我想保留这种能力。

I had a similar problem: my scripts are served by a subdomain and fall under the same origin restriction. 我遇到了类似的问题:我的脚本由子域服务,并且属于同一原点限制。 However, I solved this by: 但是,我解决了这个问题:

1) adding every script tag like this: 1)添加如下所示的每个脚本标记:

<script type="text/javascript" src="http://subdomain.mydomain.tld" crossorigin="*.mydomain.tld" />

2) modifying the apache httpd.conf by adding the following inside every vhost (you must enbable mod_headers): 2)通过在每个vhost中添加以下内容来修改apache httpd.conf(你必须使用mod_headers):

<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*.mydomain.tld"
</IfModule>

On one of my server I was not able to make this functional except by replacing 在我的一台服务器上,我无法通过更换来实现这一功能

*.mydomain.tld

by 通过

*

Be aware of the flaws with potentially allowing * to phish extended information. 请注意可能允许*对网络钓鱼扩展信息的漏洞。 Documentation on CORS, same-origin, img & fonts, cdn is available but very fewer about script tag crossorigin details is available. 有关CORS,同源,img和字体,cdn的文档可用,但有关脚本标记交叉开关详细信息的信息很少。

Hope this helps ... 希望这可以帮助 ...

Try using jsonp for the dataType attribute in jQuery.ajax . 尝试在jQuery.ajax中使用jsonp作为dataType属性。 The remote server will also need to support jsonp. 远程服务器还需要支持jsonp。 It will get around the browser security preventing XSS. 它将绕过浏览器安全阻止XSS。

Alternatively, you could use an IFrame and use jQuery within each window, but use HTML5 postMessage to communicate back and forth between the windows on two different domains. 或者,您可以使用IFrame并在每个窗口中使用jQuery,但使用HTML5 postMessage在两个不同域上的窗口之间来回通信。

Or, if you control both servers you can set the headers for same origin . 或者,如果您控制两台服务器,则可以为同一源设置标头。

Jsonp has been my weapon of choice for this kind of problem. Jsonp一直是我解决这类问题的首选武器。 The others are just a legitimate. 其他人只是合法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM