dojo 跨域访问方法
来源:互联网 发布:淘宝泳衣女装连体 编辑:程序博客网 时间:2024/05/29 14:17
Dojo FAQ: How do I use CORS with Dojo?
In web browsers that support Cross-Origin Resource Sharing (CORS) via XMLHttpRequest
objects, Dojo’s XHR mechanism can make cross-domain requests out of the box.
Because of the same-origin policy of XMLHttpRequest
, Dojo has long supported various methods of loading resources across domains – dojo/io/script
and dojo/io/frame
;dojo/request/script
and dojo/request/iframe
in recent versions (1.8+). However, modern web browsers have relaxed the same-origin policy to allow developers to perform cross-domain requests with one caveat: the server must allow cross-domain requests by responding to the request with the Access-Control-Allow-Origin
header set to a value that includes the domain of the requesting code (or *
to match all domains). If the browser supports CORS, it will complete the request as if it were a same-domain request. This feature is also available in Dojo:
require([
"dojo/request"
],
function
(request) {
request(
"http://other.domain/resource"
);
});
While Dojo’s XHR mechanism supports CORS out of the box, it sets the X-Requested-With
header by default, which will result in a pre-flighted request that may not be desirable. For requests that don’t include sensitive data or cause side effects, you can prevent the pre-flighted request by clearing the X-Requested-With
header:
require([
"dojo/request"
],
function
(request) {
request(
"http://other.domain/resource"
, {
headers: {
"X-Requested-With"
:
null
}
});
});
If you need to send HTTP authentication credentials or cookies with your cross-domain request, simply setting the withCredentials
option to true
will allow the browser’sXMLHttpRequest
to send that information:
require([
"dojo/request"
],
function
(request) {
request(
"http://other.domain/resource"
, {
headers: {
"X-Requested-With"
:
null
},
withCredentials:
true
});
});
IE8、9 如何支持
Internet Explorer 8 and 9 does not support CORS via XMLHttpRequest
, but via proprietary XDomainRequest
. Unfortunately, Dōjō does not take this into consideration and attempts to load a cross-domain resource via XHR
, which ends with the Access is denied error.
Fortunately, Dōjō provides powerful dojo/request/registry
, that allows you modify this behavior:
Define XDomainRequest provider that employs XDomainRequest
to obtain a resource:
function xdr(url, options) { var def = new Deferred(); var xdr = new XDomainRequest(); if (xdr) { xdr.onload = function(e) { def.resolve(xdr.responseText); } xdr.open(options.method, url); xdr.send(); return def; } def.reject(new Error('XDomainRequest not supported.')); return def;}
Then define corsProvider
that calls XHR
or XDR
depending on the browser:
function corsProvider(url, options) { if(window.XDomainRequest) { return xdr(url, options); } return xhr(url, options);}
Register corsProvider
to handle cross-domain requests:
var url = "http://cors-test.appspot.com/test";var handle = request.register(url, corsProvider);
Now requesting a cross-domain resource should work in IE too:
request.get(url).then(function(response) { console.log(response);});
See it in action: http://jsfiddle.net/phusick/LZZhs/
This applies for Dōjō 1.8+, because of dojo/request
. If you need the same for dojo/_base/xhr
there is dojox.io.xhrPlugins
, but I have no experience with it. Anyway, it should be quite straightforward to implement the aforementioned via dojo/aspect
for legacy Dōjō versions.
More information about IE 8/9 can found at MS sitehere.
- dojo 跨域访问方法
- dojo的跨域访问
- dojo对象方法
- DOJO 更新数据源方法
- dojo 常用方法
- dojo 基础方法介绍
- DoJo验证表单方法
- Ajax 跨域访问方法
- [摘录]跨域访问方法
- JQ跨域访问方法
- dojo常见模块及方法
- Dojo 调用 js中的方法
- dojo小例子(33)dojo.request.script通过JSONP实现跨域
- Dojo学习笔记(3. Dojo的基础对象和方法)
- Dojo学习笔记(3. Dojo的基础对象和方法)
- dojo之理解dojo.hitch及数组方法
- Dojo 学习--event(1) dojo/on与Nodelist on方法
- Dojo隐藏表单域
- Android Studio(04)——如何对APK文件签名并导出
- ROS, Pioneer, Multi-computer, interconnection
- TCP长连接与短连接的区别
- Android NDK 学习(4) 在eclipse中配置工具,方便开发
- ACM--steps--3.2.4--Humble Numbers
- dojo 跨域访问方法
- [HdOJ]5170 GTY's math problem
- YARN产生背景
- Gson 过滤字段的几种方法
- Longest Common Prefix - LeetCode
- VC 这个程序可能安装不正确,和***不兼容,既,和windows兼容性设置
- 【Java集合源码剖析】ArrayList源码剖析
- 1025_反转链表 (25)
- C语言学习三不同平台上的学习