Angular客戶端請求Rest服務(wù)跨域問題的解決方法
1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務(wù),控制臺報錯如下,但是Response為200??蛻舳撕头?wù)端IP相同,但是端口不同,存在跨域問題。
XMLHttpRequest cannot load http://localhost:8081/api/v1/staffs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
2.解決方法:在服務(wù)端/api/v1/staffs的Restful方法增加@CrossOrigin注解,比如:
@CrossOrigin(origins = "*", maxAge = 3600) @RequestMapping(value = "/api/v1/staffs", produces = { "application/json" }, method = RequestMethod.GET) RestResponseList<?> queryStaffs(@RequestParam(value = "limit", required = false, defaultValue = "20") int limit, @RequestParam(value = "offset", required = false, defaultValue = "0") int offset);
3.重新發(fā)送請求http://localhost:8081/api/v1/...,請求成功。且響應(yīng)頭增加了Access-Control-Allow-Credentials和Access-Control-Allow-Origin參數(shù)。@CrossOrigin注解即是給響應(yīng)頭增加了這兩個參數(shù)解決跨域問題。
4.在服務(wù)端POST方法同樣使用注解@CrossOrigin解決跨域問題。
@CrossOrigin(origins = "*", maxAge = 3600) @RequestMapping(value = "/api/v1/staffs", produces = { "application/json" }, method = RequestMethod.POST) RestResponse<?> createStaff(@RequestBody RestRequest<StaffReqInfo> request);
報錯如下:
5.查看響應(yīng)碼415,錯誤原因:
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported"
6.進一步查看請求頭信息,content-type為text/plain。與Response Headers的Content-Type:application/json;charset=UTF-8類型不匹配,故報錯。
7.指定請求頭content-type為application/json,比如在Angular中增加Headers。發(fā)送Post請求,請求成功。
let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers }); return this.http.post(this.staffCreateURL, body, options).map((response: Response) => { //return this.http.get(this.userLoginURL).map((response:Response)=> { let responseInfo = response.json(); console.log("====請求staffCreateURL成功并返回數(shù)據(jù)start==="); console.log(responseInfo); console.log("====請求staffCreateURL成功并返回數(shù)據(jù)end==="); let staffName = responseInfo.responseInfo.staffName; console.log(staffName); return responseInfo; })
另:也可以在HttpServletResponse對象通過setHeader("Access-Control-Allow-Origin", "*")方法增加響應(yīng)頭參數(shù),解決跨域問題,即是@CrossOrigin注解方式。推薦使用注解,方便。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基礎(chǔ) ng-list 指令詳解及示例代碼
本文主要介紹AngularJS ng-list 指令,這里幫大家整理了ng-list指令的基本資料,并附有示例代碼,有需要的小伙伴可以參考下2016-08-08AngulerJS學(xué)習之按需動態(tài)加載文件
本篇文章主要介紹了AngulerJS學(xué)習之按需動態(tài)加載文件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Angular.js回顧ng-app和ng-model使用技巧
這篇文章主要回顧Angular.js中ng-app和ng-model使用技巧,感興趣的小伙伴們可以參考一下2016-04-04利用Angular2 + Ionic3開發(fā)IOS應(yīng)用實例教程
這篇文章主要給大家介紹了關(guān)于利用Angular2 + Ionic3開發(fā)IOS應(yīng)用的相關(guān)資料,文中通過示例代碼和圖文介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。2018-01-01AngularJS報錯$apply already in progress的解決方法分析
這篇文章主要介紹了AngularJS報錯$apply already in progress的解決方法,較為詳細的分析了報錯$apply already in progress的原理、處理技巧與相關(guān)注意事項,需要的朋友可以參考下2017-01-01AngularJS的ng Http Request與response格式轉(zhuǎn)換方法
這篇文章主要介紹了AngularJS的ng Http Request與response格式轉(zhuǎn)換方法,結(jié)合實例形式分析了AngularJS實現(xiàn)Request與response格式轉(zhuǎn)換操作的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2016-11-11詳解Angular 中 ngOnInit 和 constructor 使用場景
最初學(xué)習Angular的時候總是搞不清楚ngOnInit和constructor的區(qū)別,現(xiàn)在我們來稍微理一下兩者之間的區(qū)別。2017-06-06