简体   繁体   English

如何在不使用XDomainRequest或XMLHttpRequest的情况下使用自定义标头创建跨域请求?

[英]How can I make cross-domain requests with custom headers without using XDomainRequest or XMLHttpRequest?

I have a single page web application delivered from www.example.com . 我有一个从www.example.com发送的单页Web应用程序。 This web applications 这个Web应用程序

  • needs to make AJAX requests against another server named api.example.com 需要针对名为api.example.com另一台服务器发出AJAX请求
  • it has to set certain header fields like Authorization when sending requests to api.example.com 它必须在向api.example.com发送请求时设置某些标头字段,如Authorization
  • it has to be compatible with recent and not so recent browsers (for example IE >= 8) 它必须与最近的浏览器兼容(例如IE> = 8)

All this works by handling CORS requests on api.example.com with Chrome (and other recent WebKit-based browsers) using XMLHttpRequest . 所有这一切都是通过使用XMLHttpRequest在Chrome(以及其他最近的基于WebKit的浏览器)上处理api.example.com上的CORS请求来实现的。 IE older than version 10 doesn't implement CORS for XMLHttpRequest and instead provides the non-standard XDomainRequest object for cross-domain requests. 早于版本10的IE不为XMLHttpRequest实现CORS,而是为跨域请求提供非标准XDomainRequest对象。 But XDomainRequest does not implement a way to set HTTP header fields. 但是XDomainRequest没有实现设置HTTP头字段的方法。

So my question is: How can I make cross-domain requests with custom headers without using XDomainRequest or XMLHttpRequest ? 所以我的问题是:如何在不使用XDomainRequestXMLHttpRequest情况下使用自定义标头创建跨域请求? What is the best practice workaround? 什么是最佳实践解决方法?

Edit: I have control over all involved servers (*.example.com). 编辑:我可以控制所有涉及的服务器(* .example.com)。

I'm afraid there is no other way but to implement a proxy for this request in your application's server side. 我担心除了在应用程序服务器端为此请求实现代理之外别无他法。 An example . 一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM