我正在尝试使用window.onerror处理程序将所有应用程序错误报告给我。这非常适合我域中的脚本,但不适用于从CDN加载的脚本,因为它将报告此处公开的神秘错误: Cryptic "Script Error." reported in Javascript in Chrome and Firefox
并且正如本文所述,您可以使用脚本的crossorigin属性来获取完整的跨域错误报告,但是令人惊讶!显然,即使Google基本上以他们的业务模型强迫您执行跨域脚本编写,但它们也不提供(至少是开箱即用)对子资源完整性的支持,即使Google映射。在此处详细了解此内容:link
我的问题是:是否可以通过Google控制台配置Google地图以返回适当的“ Access-Control-Allow-Origin”标头,以便我可以进行适当的错误报告?如果不是这样,我认为作为强制CDN,Google应该考虑增加对此的支持。
我在这里显示一个小的HTML示例,说明问题。启用crossorigin属性后,您会收到一条不错的消息,但由于未正确配置Google服务器,因此无法正常工作。而且在window.onerror中没有它,您将获得0条信息。
<html>
<body>
<script type="text/javascript">
window.onerror = function (msg, url, line, column, error) {
console.log(`There was an error with message:${msg} in the url: ${url} line ${line} column ${column} with error ${error}`);
//Here we will call our server with the proper error information.
};
</script>
<!-- try using the first one and you will notice that the error message that we get in the onerror callback is useless -->
<!-- <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script> -->
<!-- try using the second one and you will notice that there is an error in the console -> Access to Script at 'https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.-->
<!-- This error indicates a bad configuration on Google servers that are not returning a valid 'Access-Control-Allow-Origin' header -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer crossorigin="anonymous"></script>
</body>