作为读取 signalR 的一部分 documentation和 jQuery 的文档
Cors is equal to true if a browser can create an XMLHttpRequest object and if that XMLHttpRequest object has a withCredentials property. To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests (windows gadget, etc), set $.support.cors = true;
我知道并非所有浏览器都支持CORS
(Access-Control-Allow-Origin: *
)。
但是应该很简单,无论支持还是不支持。
我不明白这句话:
"To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests"
我在这里遗漏了什么吗?
最佳答案
通过将 $.support.cors
设置为 true,您将告诉 jQuery 和 SignalR,即使 jQuery 的功能检测表明不支持 CORS,执行 JS 的环境确实会允许跨域 XHR .
每次我看到有人将此标志设置为 true 时,它都是错误的。 如果在确实不支持 CORS 的环境中将其设置为 true,SignalR 将尝试使用 CORS 与服务器建立跨域连接(并失败),而不是使用可能有效的 JSONP .
jQuery 的功能检测(通过查找 XHR 对象上的 withCredentials 属性来工作)通常是准确的,只有不在在浏览器中运行时才可能不正确。这就是为什么给出了在桌面小部件中运行 jQuery 的示例。在这种情况下,jQuery 可能认为环境不支持 CORS,因为确实不支持,但环境仍然会允许任何跨域请求,无论响应中是否存在 Access-Control-Allow-Origin header 因为它直接在桌面上运行,而不是在浏览器中运行。
这就是环境如何既不支持cors,又允许跨域XHR请求。
关于javascript - $.support.cors 澄清吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17013023/