【问题标题】:Getting the http post canceled when subscribing from a service从服务订阅时取消 http 帖子
【发布时间】:2021-12-03 07:25:06
【问题描述】:

在这个错误中停留了一周。无法使用 Angular HttClient 向 heroku 服务器发送简单的发布请求。在主应用模块的提供程序部分中定义了所有服务。错误处理服务在发送发布请求后没有记录任何错误(此服务工作正常,我在另一个项目中测试过)。

组件在不同的模块中定义,但服务在根 app.module.ts 中定义和提供

这些组件所在的模块被导入到主应用模块中。

但是不管发帖请求被取消!!。

API 参数

 email: string 
 password: string

AuthModel

用于定义 API 数据参数的模型

export interface AuthModel {
    email: string;
    password: string
}

AuthService.ts

这是我用来注入到我的 component.ts 文件中以进行订阅的服务。该服务还使用另一个服务来处理错误情况 HandleError

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
  })
};

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  signUpUrl = 'myurl';
  
  private handleError: HandleError;


  constructor(private http: HttpClient, private httpErrorService: HttpErrorService) { 

    this.handleError = this.httpErrorService.createHandleError('AuthService'); //Problem lies here
  }

  signUp(authModel:  AuthModel) : Observable<AuthModel>
  { 
    return this.http.post<AuthModel>(this.signUpUrl,authModel,httpOptions).pipe( catchError(this.handleError('signup',authModel)) );
  }

}

component.ts

输入数据后点击按钮时调用提交函数

Submit(): void {
    
    this.authModel!.email = this.emailHolder.value;
    this.authModel!.password = this.passwordHolder.value;

    this.authService.signUp(this.authModel).subscribe((res)=> {console.log(res)});
    
  }

【问题讨论】:

  • 你是否在 app.module.ts 中添加了httpClientModule
  • 预检请求状态未知,是否也取消了? Access-Control-Allow-Origin 标头的值是多少?还有Access-Control-Allow-Methods?
  • 从哪里调用 submit() 方法?
  • 您的注册操作中似乎有switchMap
  • 这些看起来像请求标头,您应该在响应标头中搜索我提到的那些。也许在预检请求中

标签: node.js angular api rxjs httpclient


【解决方案1】:

问题出在我的 handleError 函数上,不管响应如何,它都会被取消。所以一定要写一个正确的errorHandling函数,否则你会得到意想不到的结果。

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2019-01-18
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多