springmvc 的跨域訪問
阿新 • • 發佈:2018-11-09
為什麼跨域訪問,就不多說原因了。只說解決方式。
1.服務端跨域,前端有個jsonp,用來解決跨域。在ajax提交的時候,content-type設定為jsonp就可以了。其他的跟json沒區別。
2.伺服器端解決跨域。
第一種:服務端解決跨域的話,如果用的是spring4.2以上,在要訪問的Controller上新增@CrossOrigin,基本解決跨域的問題。
第二種:配置filter,具體的配置搜一下,我也沒這樣用。放的太開了。
現在說一下問題:在第一種的情況下,添加了@crossOrigin註解後,但是,前端還是提示跨域訪問限制。如:
is therefore not allowed access. The response had HTTP status code 403;
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin;
後端也有相應的錯誤提示:
the request was rejected because no multipart boundary was found;
出現這樣的問題是因為沒有配置接收的方式,在requestMapping上配置訪問方式就好。