這種服務應該執行HTTP POST請求我的PHP後端API,但NG2不會編譯項目,它引發以下錯誤:Angular2 HTTP POST頭參數類型錯誤
ERROR in login-service.ts (25,9): Argument of type '{ headers: Headers; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'Headers'. Two different types with this name exist, but they are unrelated. Property 'keys' is missing in type 'Headers'.
我的片段看起來像:
import { HttpModule } from '@angular/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Response } from '@angular/http';
import { Headers } from '@angular/http';
@Injectable()
export class LoginService {
constructor(private http: Http) {
}
login(username, password){
var body = 'foo=bar&foo1=bar1';
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http
.post('http://127.0.0.1/server.php', body, { headers: headers })
.subscribe((data:Response) => {
let res = data.text();
console.log('Result: ',res);
}, error => {
console.log('Error: ', JSON.stringify(error.json()));
});
}
}
這段代碼完全適用於不同的ng2應用程序。
錯誤文本顯示的標頭值是'Headers'大寫字母H,但是您的代碼示例顯示了帶有小h的'Header'。請確保您準確地重現代碼和錯誤。 – BeetleJuice