場景:
系統A:nodejs+vue.js
系統B:spring boot
經過系統A前端調用系統B,一直提示:No 'Access-Control-Allow-Origin' header is present on the requested resourcejavascript
定位問題:
js跨域問題,很好定位html
嘗試解決:
網上解決方法不少,以下:
http://www.javashuo.com/article/p-vmfhiwmc-ey.html前端
後臺controller以下:vue
try{ response.setHeader("Access-Control-Allow-Origin", "*"); String request = new String(HttpUtils.stream2Bytes(requestBody.getInputStream()),"UTF-8"); catch(Exception ex){ logger.error("參數錯誤",e); return null; }
同時前端代碼以下:java
this.$http.post({ url: server, params: submitData, successCallback: (rs) => { var result = JSON.parse(message); this.addChatMsg(result.data.msg, () => { this.$refs.common_chat.goEnd(); }); } });
結果同樣,仍是報錯...node
後面反覆查看nginx, spring日誌,發現一個小細節nginx
catch(Exception ex){ logger.error("參數錯誤",e); // 這裏返回null, response.setHeader失效了 return null; // 正確應返回以下 return response; }