在 IE9 下面, XMLHttpRequest 是不支持跨域請求的. IE10 的 XMLHttpRequest
支持跨域, 而 IE8, IE9 須要使用 XDomainRequest 來實現跨域.javascript
.factory("ie9Http",["$q",function($q){ // ie9 xmlHttp 不支持cors跨域解決方案 return { fetchIe9: function (url, options) { var deffered = $q.defer(); if (window.XDomainRequest) { // ie9支持此屬性 var method = options.method || 'GET'; var timeout = options.timeout || 30000; var data = options.body || options.params || {}; if (data instanceof Object) { data = JSON.stringify(data); } var XDR = new XDomainRequest(); XDR.open(method, url); XDR.timeout = timeout; XDR.onload = function(){ try { var json = JSON.parse(XDR.responseText); deffered.resolve(json); } catch (e) { deffered.reject(e); } }; XDR.ontimeout = function(){return deffered.reject('XDomainRequest timeout');}; XDR.onerror = function(){return deffered.reject('XDomainRequest error');}; XDR.send(data); } else { deffered.reject({err:'不支持XDomainRequest對象'}); } return deffered.promise; } }; }])
參考文章: http://web.jobbole.com/94725/java