首页 > 解决方案 > Angular 7不将发布数据发送到api

问题描述

我有一个asp.net后端服务。后端 api 工作正常postman

在此处输入图像描述

但是从角度做同样的事情是行不通的。正文不会发送到服务器。我正在使用角度 7。

这是我的代码。

乳制品服务

public addNewDairy(value) {
    return this.http.post(this.apiUrl + "dairy", value)
      .pipe(
        map(success => success),
        catchError(this.hes.handleError) // then handle the error
      );
  }

和组件

requestForRegistration(value) {
    this.jqxLoader.open();
    this.ds.addNewDairy(value).subscribe(res => {
      this.jqxLoader.close();
      this.message = "Requested for activation.";
      this.notification.open();
    }, error => {
      console.log(error);
      this.jqxLoader.close();
      this.message = "Something went wrong please try again!";
      this.notification.open();
    });
  }

在此处输入图像描述

标签: angulartypescriptasp.net-web-apihttpclient

解决方案


204 来自浏览器作为跨域请求的一部分发送的 OPTIONS 请求。一旦 OPTIONS 请求以 200 成功返回,您的真实请求就会上升。Postman 不会发送 OPTIONS 请求,因为在该实例中您将直接访问 API。您的 Angular 看起来不错 - 我会在您的 asp.net 后端验证是否启用了 CORS - 请参见此处https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-请求-in-web-api#enable-cors


推荐阅读