Angular Http post请求不起作用
我正在尝试向后端RESTAPI发送http post请求。以下是http服务的代码:Angular Http post请求不起作用,angular,Angular,我正在尝试向后端RESTAPI发送http post请求。以下是http服务的代码: @Injectable() export class requestService{ constructor(private _http:Http){} postRequest(request:any){ console.log("Here is post") const body=JSON.stringify(request); let he
@Injectable()
export class requestService{
constructor(private _http:Http){}
postRequest(request:any){
console.log("Here is post")
const body=JSON.stringify(request);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this._http.post('http://localhost:8080/requests',body,options).map(res=> console.log(res.headers));
}
}
我确信我的后端工作正常。我成功地通过邮递员发送了一个邮寄请求。但是,当我使用我的服务发送请求时,什么也没有发生。没有错误。此外,对象也不会记录到后端
那么,问题是什么呢?
可观察对象是懒惰的。没有订阅,他们什么都不会订阅
使用subscribe()
而不是map()
或者返回可观察到的并在调用方站点订阅:
@Injectable()
export class requestService{
constructor(private _http:Http){}
postRequest(request:any){
console.log("Here is post")
const body=JSON.stringify(request);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:8080/requests',body,options).map(res=> console.log(res.headers));
}
}
可观察对象是惰性的。没有订阅,他们什么都不会订阅
使用subscribe()
而不是map()
或者返回可观察到的并在调用方站点订阅:
@Injectable()
export class requestService{
constructor(private _http:Http){}
postRequest(request:any){
console.log("Here is post")
const body=JSON.stringify(request);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:8080/requests',body,options).map(res=> console.log(res.headers));
}
}
试试这个--this.\u http.post('http://localhost:8080/requests映射(res=>console.log(res.data))代码>我不知道角度,但你确定你甚至捕捉到了错误吗?您在某处有错误回调吗?@micronyks它不起作用。您在控制台中有标题吗?请尝试此--this.\u http.post('http://localhost:8080/requests映射(res=>console.log(res.data))代码>我不知道角度,但你确定你甚至捕捉到了错误吗?你们在什么地方有错误回调吗?@micronyks它不工作。你们在控制台中有标题吗?
this.requestService.postRequest.subscribe(val => console.log(val));