Angular客戶端請(qǐng)求Rest服務(wù)跨域問題的解決方法
1.問題描述:通過Origin是http://localhost:4200請(qǐng)求http://localhost:8081的服務(wù),控制臺(tái)報(bào)錯(cuò)如下,但是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ā)送請(qǐng)求http://localhost:8081/api/v1/...,請(qǐng)求成功。且響應(yīng)頭增加了Access-Control-Allow-Credentials和Access-Control-Allow-Origin參數(shù)。@CrossOrigin注解即是給響應(yīng)頭增加了這兩個(gè)參數(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);
報(bào)錯(cuò)如下:

5.查看響應(yīng)碼415,錯(cuò)誤原因:
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported"
6.進(jìn)一步查看請(qǐng)求頭信息,content-type為text/plain。與Response Headers的Content-Type:application/json;charset=UTF-8類型不匹配,故報(bào)錯(cuò)。

7.指定請(qǐng)求頭content-type為application/json,比如在Angular中增加Headers。發(fā)送Post請(qǐng)求,請(qǐng)求成功。
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("====請(qǐng)求staffCreateURL成功并返回?cái)?shù)據(jù)start===");
console.log(responseInfo);
console.log("====請(qǐng)求staffCreateURL成功并返回?cái)?shù)據(jù)end===");
let staffName = responseInfo.responseInfo.staffName;
console.log(staffName);
return responseInfo;
})
另:也可以在HttpServletResponse對(duì)象通過setHeader("Access-Control-Allow-Origin", "*")方法增加響應(yīng)頭參數(shù),解決跨域問題,即是@CrossOrigin注解方式。推薦使用注解,方便。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談angular.js跨域post解決方案
- Angular4開發(fā)解決跨域問題詳解
- AngularJS實(shí)現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解
- angular.js中解決跨域問題的三種方式
- 詳解基于angular-cli配置代理解決跨域請(qǐng)求問題
- Angular實(shí)現(xiàn)跨域(搜索框的下拉列表)
- 解析AngularJS中g(shù)et請(qǐng)求URL出現(xiàn)的跨域問題
- 詳解AngularJS如何實(shí)現(xiàn)跨域請(qǐng)求
- AngularJs解決跨域問題案例詳解(簡(jiǎn)單方法)
- AngularJS iframe跨域打開內(nèi)容時(shí)報(bào)錯(cuò)誤的解決辦法
相關(guān)文章
詳解AngularJs ui-router 路由的簡(jiǎn)單介紹
本篇文章主要介紹了AngularJs ui-router 路由的簡(jiǎn)單介紹。簡(jiǎn)單明了的說明了ngRoute和ui-router的區(qū)別和用法,有興趣的可以了解一下2017-04-04
angular學(xué)習(xí)之動(dòng)態(tài)創(chuàng)建表單的方法
這篇文章主要介紹了angular學(xué)習(xí)之動(dòng)態(tài)創(chuàng)建表單的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
AngularJS基礎(chǔ) ng-keydown 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-keydown 指令,在這里幫大家整理了ng-keydown 指令的基礎(chǔ)資料,并附有代碼,有需要的朋友可以參考下2016-08-08
詳解AngularJS臟檢查機(jī)制及$timeout的妙用
本篇文章主要介紹了詳解AngularJS臟檢查機(jī)制及$timeout的妙用,“臟檢查”是Angular中的核心機(jī)制之一,它是實(shí)現(xiàn)雙向綁定、MVVM模式的重要基礎(chǔ),有興趣的可以了解一下2017-06-06

