Angular 7没有将帖子数据发送到api

问题描述 投票:0回答:1

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

enter image description here

但从角度做同样的事情是行不通的。正文不会发送到服务器。我正在使用角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();
    });
  }

enter image description here

angular typescript asp.net-web-api httpclient
1个回答
0
投票

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-requests-in-web-api#enable-cors

© www.soinside.com 2019 - 2024. All rights reserved.