V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
97world

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

  •  
  •   97world · Jul 15, 2017 · 2649 views
    This topic created in 3222 days ago, the information mentioned may be changed or developed.

    如果代码中有不同域的请求发出, 浏览器因为同源策略的原因会拦截该请求并在 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 replies    2017-08-18 13:16:57 +08:00
    yyfearth
        1
    yyfearth  
       Jul 15, 2017 via iPhone
    Ajax 错误本来就是 window.onerror 捕捉不到的呀 Ajax 错误应该是 xhr 的 onerror
    Chrome console 里面显示的 大都 onerror 捉不到的 比如 console log 就不是 onerror
    dou4cc
        2
    dou4cc  
       Aug 18, 2017
    @yyfearth 然而 firefox 里什么都会冒泡上去
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   843 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    ♥ Do have faith in what you're doing.