角度删除 - API 未收到调用

我有以下自动生成的代码 (NSwag Studio) - 唯一的修改是withCredentials我为 Windows Auth(内网应用程序)添加的代码。


    deleteObjective(id: number): Observable<ObjectiveDTO> {

        let url_ = this.baseUrl + "/api/Objectives/{id}";

        if (id === undefined || id === null)

            throw new Error("The parameter 'id' must be defined.");

        url_ = url_.replace("{id}", encodeURIComponent("" + id)); 

        url_ = url_.replace(/[?&]$/, "");


        let options_ : any = {

            observe: "response",

            responseType: "blob",

            withCredentials: true,

            headers: new HttpHeaders({

                "Accept": "application/json"

            })

        };


        return this.http.request("delete", url_, options_).pipe(_observableMergeMap((response_ : any) => {

            return this.processDeleteObjective(response_);

        })).pipe(_observableCatch((response_: any) => {

            if (response_ instanceof HttpResponseBase) {

                try {

                    return this.processDeleteObjective(<any>response_);

                } catch (e) {

                    return <Observable<ObjectiveDTO>><any>_observableThrow(e);

                }

            } else

                return <Observable<ObjectiveDTO>><any>_observableThrow(response_);

        }));

    }

生成的服务中的其他一切工作正常(这是唯一的删除),但这实际上没有发送任何流量 - 在 Chrome 网络拉出(F12)中,没有对 API 的调用,API 也没有收到任何内容。


我从我的组件中调用它,如下所示:


  deleteObjective(): void {

    if (confirm('Are you sure you want to delete this objective? This cannot be un-done.')) {

      if (this.objective.id !== 0)

        this.service.deleteObjective(this.objective.id);


      for (var i = 0; i < this.objectives.length; i++) {

        if (this.objectives[i] === this.objective) {

          this.objectives.splice(i, 1);

        }

      }

    }

  }

并且拼接肯定是有效的。如果我放在debuggerhttp 请求之前,它会调用它。控制台中没有错误。


有任何想法吗?我是 angular 的新手,但对编程很老。


蓝山帝景
浏览 203回答 2
2回答

梵蒂冈之花

当您从服务调用该函数时,请确保您subscribe()使用它。

慕标琳琳

订阅后才会命中API像这样尝试:&nbsp; deleteObjective(): void {&nbsp; &nbsp; if (confirm('Are you sure you want to delete this objective? This cannot be un-done.')) {&nbsp; &nbsp; &nbsp; if (this.objective.id !== 0)&nbsp; &nbsp; &nbsp; &nbsp; this.service.deleteObjective(this.objective.id).subscribe(res => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < this.objectives.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.objectives[i] === this.objective) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.objectives.splice(i, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript