javascript CORS onerror handler [英] javascript CORS onerror handler

查看:280
本文介绍了javascript CORS onerror handler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

Background


  • 几年前, onerror 处理程序和跨原始脚本标签,

  • A few years ago there was an issue with the onerror handler and cross origin script tags, more info on that.

主要浏览器修正了问题。

Major browsers fixed the issue.

知道这是从CDNed脚本检测客户端错误的一个问题,他们有点放松了这些约束( firefox webkit

Knowing this was a problem with detecting client side errors from CDNed scripts, they somewhat relaxed these constraints (firefox, webkit)

实际问题

我在localhost上托管一个简单的页面,并包含来自不同域(例如sitea)的脚本,下面是HTML的样子:

I'm hosting a simple page on localhost and including a script from a different domain (e.g. "sitea"), here's what the HTML looks like:

<html>
<head>
<script>window.onerror = function(e, f, g) { console.log('err',e,f,g) }</script>
</head>
<body><h2>test</h2>
<script src='http://siteA:8081/one.js' crossorigin='anonymous'></script>
</body>
</html>

siteA 上的脚本执行此操作: / p>

The script on siteA does this:

var foo; foo.bar;

显然这将会抛出,因为bar未定义。

Obviously that's going to throw since bar is undefined.

不幸的是,我仍然在第0行获取脚本错误,如门票中所述。

Unfortunately I'm still getiing the "Script Error" at line 0, like described in the tickets.

请注意,我是:


  • 设置跨域属性。

  • Setting the crossdomain attribute.

查看

将Access-Control-Allow-Origin标头设置为*,并在dev网络工具上查看。

Setting the Access-Control-Allow-Origin header to "*" and seeing it on the dev web tools.

我已经尝试过在Firefox和chrome,它不工作。任何人都知道为什么?

I've tried it both in firefox and chrome, it doesn't work. Anyone has an idea why?

推荐答案

适用于我在Firefox 20和Chrome 支持跨版本30的错误报告

您可以看到浏览器确实发送了 Origin:标签,因为< script> 标签具有 crossorigin 属性,因为服务器响应 Access-Control-Allow-Origin:*

You can see that indeed the browsers do send an Origin: header because the <script> tag has a crossorigin attribute, and the error is properly reported because the server responds with Access-Control-Allow-Origin: *.

这篇关于javascript CORS onerror handler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆