Detect browser support for cross-domain XMLHttpRequests?(检测浏览器对跨域 XMLHttpRequests 的支持?)
问题描述
我正在开发一些 Javascript,它利用 Firefox 3.5 执行跨域 XMLHttpRequests 的能力……但如果它们不受支持,我想优雅地失败.
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 和歌剧.
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 的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测浏览器对跨域 XMLHttpRequests 的支持?


基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01