溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決Angular客戶端請求Rest服務(wù)跨域的問題

發(fā)布時(shí)間:2021-07-16 14:59:22 來源:億速云 閱讀:193 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了如何解決Angular客戶端請求Rest服務(wù)跨域的問題,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1.問題描述:通過Origin是http://localhost:4200請求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ā)送請求http://localhost:8081/api/v1/...,請求成功。且響應(yīng)頭增加了Access-Control-Allow-Credentials和Access-Control-Allow-Origin參數(shù)。@CrossOrigin注解即是給響應(yīng)頭增加了這兩個(gè)參數(shù)解決跨域問題。

如何解決Angular客戶端請求Rest服務(wù)跨域的問題

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ò)如下:

如何解決Angular客戶端請求Rest服務(wù)跨域的問題

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)一步查看請求頭信息,content-type為text/plain。與Response Headers的Content-Type:application/json;charset=UTF-8類型不匹配,故報(bào)錯(cuò)。

如何解決Angular客戶端請求Rest服務(wù)跨域的問題

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成功并返回?cái)?shù)據(jù)start===");
 console.log(responseInfo);
 console.log("====請求staffCreateURL成功并返回?cái)?shù)據(jù)end===");
 let staffName = responseInfo.responseInfo.staffName;
 console.log(staffName);
 return responseInfo;
})

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

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何解決Angular客戶端請求Rest服務(wù)跨域的問題”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI