国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Angular客戶端請求Rest服務跨域問題的解決方法

來源:懂視網 責編:小采 時間:2020-11-27 22:29:37
文檔

Angular客戶端請求Rest服務跨域問題的解決方法

Angular客戶端請求Rest服務跨域問題的解決方法:1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務,控制臺報錯如下,但是Response為200。客戶端和服務端IP相同,但是端口不同,存在跨域問題。 代碼如下: XMLHttpRequest cannot load http
推薦度:
導讀Angular客戶端請求Rest服務跨域問題的解決方法:1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務,控制臺報錯如下,但是Response為200。客戶端和服務端IP相同,但是端口不同,存在跨域問題。 代碼如下: XMLHttpRequest cannot load http

1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務,控制臺報錯如下,但是Response為200。客戶端和服務端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.解決方法:在服務端/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.重新發送請求http://localhost:8081/api/v1/...,請求成功。且響應頭增加了Access-Control-Allow-Credentials和Access-Control-Allow-Origin參數。@CrossOrigin注解即是給響應頭增加了這兩個參數解決跨域問題。

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。發送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成功并返回數據start===");
 console.log(responseInfo);
 console.log("====請求staffCreateURL成功并返回數據end===");
 let staffName = responseInfo.responseInfo.staffName;
 console.log(staffName);
 return responseInfo;
})

另:也可以在HttpServletResponse對象通過setHeader("Access-Control-Allow-Origin", "*")方法增加響應頭參數,解決跨域問題,即是@CrossOrigin注解方式。推薦使用注解,方便。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Angular客戶端請求Rest服務跨域問題的解決方法

Angular客戶端請求Rest服務跨域問題的解決方法:1.問題描述:通過Origin是http://localhost:4200請求http://localhost:8081的服務,控制臺報錯如下,但是Response為200。客戶端和服務端IP相同,但是端口不同,存在跨域問題。 代碼如下: XMLHttpRequest cannot load http
推薦度:
標簽: 解決 請求 客戶端
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 一区二区三区网站 | 一级毛片免费观看久 | 欧美韩日| 亚洲精品免费观看 | 国产精欧美一区二区三区 | 女人18毛片a级毛片一区②区 | 国产欧美日韩综合精品无毒 | 干比网| 一本久久精品一区二区 | 国产精品区一区二区三 | 国产九九精品 | 欧美成人免费高清二区三区 | 亚洲国产精品婷婷久久 | 亚洲欧洲高清 | 中文字幕一区二区三区在线观看 | 99久久99久久久精品久久 | 手机看片日韩欧美 | 三级第一页 | 精品国产亚洲一区二区三区 | 欧美午夜视频在线观看 | 亚洲国产一成人久久精品 | 欧美亚洲网站 | 国产91精品久久久久999 | 国产成人一区二区三区小说 | 日韩视频在线免费观看 | 欧美日韩国产在线观看 | 欧美视频亚洲色图 | 国内精品久久久久久中文字幕 | 欧美精品国产综合久久 | 婷婷久久综合 | 免费一区二区三区免费视频 | 欧美日韩视频一区二区三区 | 欧美成人免费高清视频 | 欧美v在线 | 久久91精品国产91久久 | 国产日韩一区二区三区 | 国模双双大尺度炮交g0go | 欧美另类在线观看 | 在线观看色 | 日韩电影第一页 | 99久久精品免费国产一区二区三区 |