• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答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
    主站蜘蛛池模板: 伊人久久无码精品中文字幕| 欧美精品成人3d在线| 国产综合精品久久亚洲| 国产精品1区2区| 精品人妻无码专区中文字幕| 国产精品视频白浆免费视频| 国内精品人妻无码久久久影院| 最新亚洲精品国自产在线观看| 国产精品无码AV一区二区三区| 久久久久久国产精品免费无码 | 亚洲国产成人久久精品动漫 | 精品人妻中文av一区二区三区| 亚洲精品WWW久久久久久 | 国产精品亚洲专区无码WEB| 日韩精品无码免费视频| 精品国产亚洲一区二区三区| 四虎国产精品永久在线观看| 91精品国产麻豆国产自产在线| 国产网红无码精品视频| 国产精品视频不卡| 欧美国产日韩精品| 国产福利精品视频自拍| 国产AV无码专区亚洲精品| 亚洲综合无码精品一区二区三区| 久久国产精品偷99| 国产在线精品一区二区夜色| 国产精品美女WWW爽爽爽视频| 最新国产精品亚洲| 91不卡在线精品国产| 91po国产在线精品免费观看| 国产精品成人99久久久久91gav | 亚洲精品国偷自产在线| 亚洲精品成人在线| 亚洲日韩欧美制服精品二区| 无夜精品久久久久久| 亚洲国产精品成人久久蜜臀| 亚洲精品线路一在线观看| 亚洲av午夜成人片精品网站| 精品永久久福利一区二区| 国产精品久久午夜夜伦鲁鲁| 国产精品天天影视久久综合网|