使用jq的話,設置ajax這兩個屬性便可。javascript
第一個爲使用crossDomain,第二個屬性使其能夠傳輸cookiehtml
$.ajaxSetup({ crossDomain: true, xhrFields: { withCredentials: true } });
插件地址:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest但這種方式IE9並不支持,這時候強大的jq插件庫的做用就顯示出來了java
同時後臺還須要進行設置,spring中直接在Controller上加上@CrossOrigin註解便可。git
若是失效,請爲Controller設置具體的methodgithub
@CrossOrigin @RequestMapping(value = "/Controller", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
若是不用spring需自行設置下面三個頭信息ajax
Access-Control-Allow-Origin Access-Control-Allow-Credentials Access-Control-Expose-Headers
angularJS $http設置全局的跨域支持:spring
angular.module('myApp') .config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.withCredentials = true; }]) api.checkNoteBookFile = function(url) { return $http({ method: 'GET', url: url, crossDomain: true, withCredentials: false, headers: {'Content-Type': 'application/x-www-form-urlencoded'} }); }