【问题标题】:angular, Java : HTTP request not going to server, while URL is valid角度,Java:HTTP 请求不会发送到服务器,而 URL 有效
【发布时间】:2021-06-29 11:45:46
【问题描述】:

我在一个应用程序中工作:Java 后端和 Angular 前端。我正在使用 angular Fromly,数据正在进入服务,但从服务中它不会进入服务器。

让我们分享代码片段:

服务代码:

    export class RecommendationRequestService {
        readonly ROOT_URL = environment.apiUrl + '/am/v1/recommendation-requests';
    
        constructor(private http: HttpClient, private configService: RecommenderConfigService) {
        } ​
   ​
       ​updateData(interviewStatus: InterviewStatusRecommendation): Observable<any> {
           ​console.log(interviewStatus);
           ​return this.http.put<any>(this.ROOT_URL, interviewStatus);
       ​}
   ​}

这一行正在打印预期的数据集:console.log(interviewStatus); 服务器正在运行。

调用服务的代码:

onSubmit() {
    this.model.recommendationRequest.agentInitiationId = this.agentInitiationId;
    const subs = this.service.updateData(this.model).subscribe(response => {
            console.log('------' + response);
            if (response === 'OK') {
                this.notify.success('Request Recommendation Update success.');
            } else {
                this.notify.error('Request Recommendation Update fail.');
            }
        },
        err => {
            if (err.error.hasOwnProperty('code') && err.error.code === 1000) {
                this.notify.error(CommonEnum.VALIDATION_ERROR);
            }
        });
    subs.unsubscribe();
}

console.log('------' + response); 这一行应该至少打印出-----,但是什么也没有。

我从浏览器检查了网络监视器,没有通话。

可能是什么问题,来自fromly 的任何内容?

【问题讨论】:

  • 你检查控制台了吗?有什么错误吗?
  • 假设其他请求确实到达了您的服务器,我可能会先尝试删除subs.unsubscribe()。真的只是一种预感,但我觉得在调用方法中立即取消订阅是不正确的。

标签: java angular angular-formly


【解决方案1】:

您的做法不正确,因为 Aldin Bradaric 也在评论中更新了,一旦您在下一刻拨打电话,您就取消订阅了。这是你应该做的:

public subs: [] = [];
onSubmit() {
    this.model.recommendationRequest.agentInitiationId = this.agentInitiationId;
    const subs = this.service.updateData(this.model).subscribe(response => {
            console.log('------' + response);
            if (response === 'OK') {
                this.notify.success('Request Recommendation Update success.');
            } else {
                this.notify.error('Request Recommendation Update fail.');
            }
        },
        err => {
            if (err.error.hasOwnProperty('code') && err.error.code === 1000) {
                this.notify.error(CommonEnum.VALIDATION_ERROR);
            }
        });
    //subs.unsubscribe();   // remove it and add it to the lifecycle hooks
    this.subs.push(subs);

}
ngOnDestroy() {
     // create an array of subscription
     this.subs.forEach(sub => sub.unsubscribe() )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多