您好,登錄后才能下訂單哦!
最近小編在學(xué)習(xí)react,在學(xué)習(xí)過程中遇到React+Spring實現(xiàn)跨域問題,下面小編記錄了整個問題過程,給大家做個參考。
react 跨域訪問后臺,默認(rèn)是有跨域問題,并且火弧和谷歌瀏覽器,對跨域問題展示還不一樣.
谷歌瀏覽器如下圖:
此處狀態(tài)是200,然而在Response卻沒有任何信息,如下圖
然而火弧瀏覽器,對該問題的描述,就清淅得多,
火弧瀏覽器告訴我們,跨域了,關(guān)于react跨域的帖子,網(wǎng)上也有相關(guān)帖子,搜索到的方法,大約都是如下解決方式:
如果你是通過creat-react-app構(gòu)建的項目,請在package.json文件中的根目錄下,添加"proxy": "http://api.xxxx.com",如果你的項目,需要調(diào)用多個不同ip的接口,請使用如下配置:
"proxy": { "/api/RoomApi": { "target": "http://open.douyucdn.cn", "changeOrigin":true }, "/api/v1":{ "target":"http://capi.douyucdn.cn", "changeOrigin":true } }
配置完成后,再次訪問接口,還是出現(xiàn)一樣的跨域問題,既然recat的配置,未解決跨域問題,我就把思路轉(zhuǎn)到spring,在spring去處理跨域,
package com.gg.interceptor; import java.util.ArrayList; import java.util.List; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class ProcessInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception { // TODO Auto-generated method stub // 指定白名單域名 http://localhost:8000,http://localhost:8000 List<String> whileList = new Vector<String>(); whileList.add("http://127.0.0.1:8000"); whileList.add("http://localhost:8000"); String clientIp = httpServletRequest.getHeader("origin"); boolean status = false; for(String ip : whileList) { if(clientIp!=null&&clientIp.equals(ip)) { status = true; break; } } /** * 網(wǎng)上解決方案是httpServletResponse.setHeader("Access-Control-Allow-Origin","*");設(shè)置后發(fā)現(xiàn),還是不能處理跨域問題,需要指定某一個ip,如:http://127.0.0.1:8000 * */ httpServletResponse.setHeader("Access-Control-Allow-Origin",status?clientIp:null); //響應(yīng)頭設(shè)置 httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); //響應(yīng)類型 httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); httpServletResponse.setHeader("X-Powered-By","Jetty"); httpServletResponse.setHeader("Access-Control-Allow-Credentials","true"); String method= httpServletRequest.getMethod(); if (method.equals("OPTIONS")){ httpServletResponse.setStatus(200); return false; } System.out.println(method+",status:"+status+",clientIp:"+clientIp); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }
react客戶端代碼如下:
Model層js代碼:
*login({ payload }, { call, put }){ let formData = new FormData(); formData.append("loginId",payload.loginId);//賬號 formData.append("passWord",payload.passWord);//密碼 const response = yield call(requestGuangGao, formData); yield put({ type: 'changeLoginStatus', payload: response, }); },
api層js代碼:
export async function requestGuangGao(formData){ // let formData = new FormData(); // formData.append("loginId",params.loginId); // formData.append("passWord",params.passWord); console.log("requestGua >url :" ); return request('http://127.0.0.1:8080/guanggao/userController/login.do', { method: 'POST', mode: 'cors', body:formData, }); }
通過以下設(shè)置,react跨域問題就處理好了。
總結(jié)
以上所述是小編給大家介紹的React+Spring實現(xiàn)跨域問題的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。