Detect browser support for cross-domain XMLHttpRequests?

前端 未结 4 1005
北荒
北荒 2020-12-08 02:13

I\'m working on some Javascript that makes use of Firefox 3.5\'s ability to perform cross-domain XMLHttpRequests… But I\'d like to fail gracefully if they aren\'t supported.

4条回答
  •  生来不讨喜
    2020-12-08 02:35

    According to http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ you should be able to use:

    if ('withCredentials' in new XMLHttpRequest()) {
        /* supports cross-domain requests */
    }
    

    (Note: there is a comment on that page that Chrome 2 fails this test [although it does support cross-domain requests]. I tested Chrome 3 and the test is now passing.)

    Keep in mind that just because the browser might support the cross-domain API does not mean the target server will allow the request to complete.

提交回复
热议问题