检测跨域XMLHttpRequests的浏览器支持?

编程入门 行业动态 更新时间:2024-10-25 23:21:10
本文介绍了检测跨域XMLHttpRequests的浏览器支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一些使用Firefox 3.5的能力来执行跨域XMLHttpRequests的JavaScript。但是如果不支持,我想要优雅地失败。

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.

除了实际发出跨域请求,是否有任何方法可以检测浏览器对它们的支持?

Apart from actually making a cross-domain request, is there any way to detect a browser's support for them?

推荐答案

为了将来参考,完整的CORS功能检测应该看起来像这样:

For future reference, full CORS feature detection should look something like this:

//Detect browser support for CORS if ('withCredentials' in new XMLHttpRequest()) { /* supports cross-domain requests */ document.write("CORS supported (XHR)"); } else if(typeof XDomainRequest !== "undefined"){ //Use IE-specific "CORS" code with XDR document.write("CORS supported (XDR)"); }else{ //Time to retreat with a fallback or polyfill document.write("No CORS Support!"); }

您可以使用JSBin尝试这个测试,并看到在IE,Firefox,Chrome,Safari和Opera正确的响应。

You can try this test live using JSBin and see the proper response in IE, Firefox, Chrome, Safari, and Opera.

在非浏览器环境中有一些边缘情况,确实支持跨域XHR,但不支持XHR2 / CORS。此测试不考虑这些情况。

There are some edge cases in non-browser environments that do support cross-domain XHR but not XHR2/CORS. This test does not account for those situations.

更多推荐

检测跨域XMLHttpRequests的浏览器支持?

本文发布于:2023-08-02 18:55:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1280268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   XMLHttpRequests

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!