Angular客戶端請求Rest服務跨域問題的解決方法
1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務,控制臺報錯如下,但是Response為200??蛻舳撕头斩薎P相同,但是端口不同,存在跨域問題。
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.解決方法:在服務端/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/...,請求成功。且響應頭增加了Access-Control-Allow-Credentials和Access-Control-Allow-Origin參數(shù)。@CrossOrigin注解即是給響應頭增加了這兩個參數(shù)解決跨域問題。
4.在服務端POST方法同樣使用注解@CrossOrigin解決跨域問題。
@CrossOrigin(origins = "*", maxAge = 3600) @RequestMapping(value = "/api/v1/staffs", produces = { "application/json" }, method = RequestMethod.POST) RestResponse<?> createStaff(@RequestBody RestRequest<StaffReqInfo> request);
報錯如下:
5.查看響應碼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", "*")方法增加響應頭參數(shù),解決跨域問題,即是@CrossOrigin注解方式。推薦使用注解,方便。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Angular.js回顧ng-app和ng-model使用技巧
這篇文章主要回顧Angular.js中ng-app和ng-model使用技巧,感興趣的小伙伴們可以參考一下2016-04-04利用Angular2 + Ionic3開發(fā)IOS應用實例教程
這篇文章主要給大家介紹了關于利用Angular2 + Ionic3開發(fā)IOS應用的相關資料,文中通過示例代碼和圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01AngularJS報錯$apply already in progress的解決方法分析
這篇文章主要介紹了AngularJS報錯$apply already in progress的解決方法,較為詳細的分析了報錯$apply already in progress的原理、處理技巧與相關注意事項,需要的朋友可以參考下2017-01-01AngularJS的ng Http Request與response格式轉換方法
這篇文章主要介紹了AngularJS的ng Http Request與response格式轉換方法,結合實例形式分析了AngularJS實現(xiàn)Request與response格式轉換操作的相關設置與使用技巧,需要的朋友可以參考下2016-11-11詳解Angular 中 ngOnInit 和 constructor 使用場景
最初學習Angular的時候總是搞不清楚ngOnInit和constructor的區(qū)別,現(xiàn)在我們來稍微理一下兩者之間的區(qū)別。2017-06-06