V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
97world
V2EX  ›  JavaScript

某些错误消息 window.onerror 捕获不到, 例如触发浏览器同源策略而被拦截请求的错误消息.

  •  
  •   97world · 2017-07-15 00:34:40 +08:00 · 2037 次点击
    这是一个创建于 2471 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果代码中有不同域的请求发出, 浏览器因为同源策略的原因会拦截该请求并在 Console 中打印出错误信息.

    例如 "XMLHttpRequest cannot load http://dtoweb.com/post/cross-origin-solution-summary.html. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."

    但是使用 window.onerror 并不能捕获到这个错误消息, 像这种类型的错误, 有办法可以捕捉到吗?

    问题来了, 存在 window.onerror 捕捉不到的错误吗?

    2 条回复    2017-08-18 13:16:57 +08:00
    yyfearth
        1
    yyfearth  
       2017-07-15 01:22:58 +08:00 via iPhone
    Ajax 错误本来就是 window.onerror 捕捉不到的呀 Ajax 错误应该是 xhr 的 onerror
    Chrome console 里面显示的 大都 onerror 捉不到的 比如 console log 就不是 onerror
    dou4cc
        2
    dou4cc  
       2017-08-18 13:16:57 +08:00
    @yyfearth 然而 firefox 里什么都会冒泡上去
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   945 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:22 · PVG 06:22 · LAX 15:22 · JFK 18:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.