CORS跨域問題常用解決方法代碼實例
一 后端服務(wù)器使用過濾器
新建過濾器:
/** * 解決跨域 */ public class AccessControlAllowOriginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("解決跨域請求"); HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Access-Control-Allow-Origin", "*");//允許所有網(wǎng)站跨域訪問 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Allow-Credentials", "true"); //這里如果前端請求header首字母是小寫也是不行得,所以大小寫都寫上就沒問題了 response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin,content-type,x-requested-with,Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With"); filterChain.doFilter(servletRequest, response); } @Override public void destroy() {} }
前端header需要添加:
$.ajax( { url : 'http://c2.zhuzher.com/pdm/know/active?hotelid=808047&sdate=2019-11-09&edate=2019-11-11', beforeSend: function (xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); //設(shè)置跨域訪問信息 xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8"); }, type : 'get', dataType : 'json', data:{}, success : function(data) { alert(1111); } });
二 后端接口springboot/springmvc使用注解
springMVC的版本要在4.2或以上版本才支持@CrossOrigin ;
方法需要指明Get或者POST才行:
三 本地nginx反向代理(推薦)
本地下載解壓nginx,添加一個server配置文件:
注意,如果是放在nginx的html目錄下一般是不需要加跨域配置的,否則會報配置多余錯誤
每次可先直接使用試試,不行再加下面add_header等配置.
###start跨域支持配置#### add_header Access-Control-Allow-Origin '*'; add_header Access-Control-Allow-Headers Accept,Origin,X-Requested-With,Content-Type,If-Modified-Since,Last-Modified,Content-Length,Content-Range,Range,Content-Description,Content-Disposition; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; add_header Access-Control-Request-Headers Content-Disposition; add_header Access-Control-Allow-Credentials true; ###end ### server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #自定義本地路徑,代理轉(zhuǎn)發(fā)請求 location /pdm { proxy_pass http://c2.zhuzher.com/pdm; } } server { listen 8081; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #自定義本地路徑,代理轉(zhuǎn)發(fā)請求 location /pdm { proxy_pass http://c2.zhuzher.com/pdm; charset utf-8; # proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
項目里面直接調(diào)用配置的8081端口就可以了:
api.get('//localhost:8081/pdm/user/login',data)
注意這里還有一點需要注意,如果Content-Type是 application/json的話是無法發(fā)送跨域請求的,這里提供一種解決辦法,就是接口前端請求type改成
'Content-Type':'text/plain'
發(fā)送數(shù)據(jù)轉(zhuǎn)成字符串:
JSON.stringify(data)
后端接口用String接受數(shù)據(jù),然后再轉(zhuǎn)成對象就可以了:
@PostMapping("/distributeBatch") public ResMsg distributeSaleBatch(@RequestBody String params){ System.out.println(params); //Integer user_id, Integer customer_id //Gson 字符串轉(zhuǎn)對象 List<Map<String, Integer>> fromJson = new Gson().fromJson(params, new TypeToken<List<Map<String, Integer>>>() { }.getType()); System.out.println(new Gson().toJson(fromJson)); return registeredCustomerService.distributeSaleBatch(fromJson); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中使用Files.readLines()處理文本中行數(shù)據(jù)方式
這篇文章主要介紹了java中使用Files.readLines()處理文本中行數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java操作另一個Java程序使其重啟的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狫ava操作另一個Java程序使其重啟的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03JAVA 中實現(xiàn)整句漢字拆分、轉(zhuǎn)換為ASCII實例詳解
這篇文章主要介紹了JAVA 中實現(xiàn)整句漢字拆分、轉(zhuǎn)換為ASCII實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Java將字節(jié)轉(zhuǎn)換為十六進制代碼分享
我們知道,在java中,一個byte 就是一個字節(jié),也就是八個二進制位;而4個二進制位就可以表示一個十六進制位,所以一個byte可以轉(zhuǎn)化為2個十六進制位。下面我們就來詳細看下具體方法吧。2016-01-01