簡體   English   中英

Angular2:從HTTP發布檢測錯誤

[英]Angular2 : detect error from HTTP post

我無法攔截來自HTTP帖子的錯誤

我的mservice的一部分(http發布方法)

addApplicationLink(applicationLink: ApplicationLink){
    let body = JSON.stringify(applicationLink);
    let requestHeaders = new Headers();
    var headers = new Headers();
    headers.set('Content-Type',  ['application/json; charset=utf-8']);
     let reqoptions =  new RequestOptions({
        headers: headers
    });
    return this._http.post(this._applicationLinksUrl + this._linkServicePath,body,{headers: headers});

在我的組件中:

addApplicationLink() {
    //todo
    this.addNewLink = false;
    /* check if must be done after call real rest service */
    //this.applicationLinks.push(this.applicationLinkAdd);
    this._applicationLinkService.addApplicationLink(this.applicationLinkAdd)

        .subscribe(data => {
            this.applicationLinks.push(this.applicationLinkAdd)
        },
        error => {
            // handle error
            console.error('this an erreor ' + error.status)
        }
        )

當用戶嘗試添加兩個相同的應用程序鏈接時,后端返回錯誤409,但是當我執行時,error.status在瀏覽器控制台中顯示200,我在瀏覽器控制台中也看到XMLHttpRequest無法加載http:// localhost:7001 ......所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問源' http:// localhost:3000 '。 響應的HTTP狀態碼為409。

rem:Http帖子是使用json編寫的,因此存在優先調用

您有想法攔截錯誤409嗎?

實際上,您的服務器不會發送回CORS標頭(缺少Access-Control-Allow-Origin'標頭)。 這樣可以防止瀏覽器向瀏覽器中的Angular2應用程序提供實際的409錯誤。

您需要先解決服務器上的問題,然后才能看到此409錯誤。

有關CORS的工作方式的更多詳細信息,請查看本文:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM