财产' requestOptions'在类型上不存在

时间:2018-01-23 04:51:44

标签: angularjs angular http post

我想从Angular Web客户端向WebAPI服务发送数据。在这篇文章中,我遇到了编译错误。' [ts]属性' requestOptions'类型' UserService'(Current Typescrpipt)上不存在。

Class A

2 个答案:

答案 0 :(得分:1)

使用HttpClientModule及其HttpClient抽象。此外,您不需要使用JSON.stringify - 它是额外的

@Injectable()
export class UserService {

   constructor(private httpClient: HttpClient) { }

   RegisterUser(user: User) {

      const headerDict = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Access-Control-Allow-Headers': 'Content-Type',
      };

      const requestOptions = {                                                                                                                                                                                 
          headers: new HttpHeaders(headerDict), 
      };

      return this.httpClient.post("http://localhost:53269/Api/RegisterUser", user, requestOptions);   
  }    

}

一个注意事项。与实际问题无关。不要从rxjs/Rx导入任何内容。这将使你的捆绑巨大。如果您要导入Observable - 只需导入

import { Observable } from 'rxjs/Observable'

如果您需要某个运营商,请将其从rxjs/operatorsrxjs/observables/

单独导入

HttpClientModule使用eihter HttpClientHttpModule使用Http - 现已弃用

答案 1 :(得分:0)

你可以试试这个

  const headersOption = new HttpHeaders().set('Content-Type': 'application/json',
    'Accept': 'application/json',
    'Access-Control-Allow-Headers': 'Content-Type')

  const data = JSON.stringify(User);
  return this.http.post("http://localhost:53269/Api/RegisterUser", data, {headers:headersOption});
相关问题