[英]Property 'requestOptions' does not exist on type
I want to send data to WebAPI service from Angular web client .In this post method im getting an compile error .'[ts] Property 'requestOptions' does not exist on type 'UserService'(Current Typescrpipt) . 我想将数据从Angular Web客户端发送到WebAPI服务。在此post方法中,我收到了编译错误。'[ts]属性'requestOptions'在'UserService'类型上不存在(Current Typescrpipt)。
import { Injectable } from '@angular/core';
import { Http,Response } from '@angular/http';
import { Observable } from 'rxjs/Rx'; ///Allow clients to subscribbe responces asnc and add Rx when handling errors catch block
import {User} from './user';
import {HttpClientModule} from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import 'rxjs/Rx';
@Injectable()
export class UserService {
constructor( private http : Http) {
}
RegisterUser(user :User) {
const headerDict = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
}
const requestOptions = {
headers: new HttpHeaders(headerDict),
};
const data = JSON.stringify(User);
return this.http.post("http://localhost:53269/Api/RegisterUser", data, this.requestOptions );
}
}
Use HttpClientModule
with its HttpClient
abstraction. 使用
HttpClientModule
及其HttpClient
抽象。 Also you don't need to use JSON.stringify
- it is extra 另外,您不需要使用
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);
}
}
One note. 一注。 Not related to the actual issue.
与实际问题无关。 Don't import anything from
rxjs/Rx
. 不要从
rxjs/Rx
导入任何内容。 This will make your bundle huge. 这将使您的捆绑包变得巨大。 If you want to import
Observable
- just import like 如果要导入
Observable
只需像导入
import { Observable } from 'rxjs/Observable'
If you want some operator - import it separately from rxjs/operators
or rxjs/observables/
如果您需要一些运算符,请与
rxjs/operators
或rxjs/observables/
分开导入
Use eihter HttpClientModule
with HttpClient
or HttpModule
with Http
- which is deprecated now 使用带有
HttpClient
eihter HttpClientModule
或带有Http
HttpModule
现在已弃用
you can try this 你可以试试这个
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});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.