当我在节点服务器上打印请求的内容时,我无法在任何地方看到用户数据 .
这是我的节点服务器:
var http = require('http');
http.createServer( function (request, response) {
console.log(request);
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
这是Angular2代码:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Http, Response, Headers , RequestOptions } from "@angular/http";
import 'rxjs/add/operator/retry'; // to be able to retry when error occurs
import { Observable } from "rxjs/Rx";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'Angular Test';
user = { id : 1, name : "Hello"};
constructor (private http: Http) {}
ngOnInit(): void {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
console.log(this.user);
this.http.post("http://localhost:8080/", this.user, options)
.subscribe(
(err) => {
if(err) console.log(err);
console.log("Success");
});
}
}
任何人都可以帮我解释或解释如何处理角度的http请求 .
2 回答
那是你的服务器:
那是你的角客户端:
回购https://github.com/kuncevic/angular-httpclient-examples
我已经在我们的文档页面中写了这个,但由于现在已经弃用了,我会在这里复制它 .
您的节点部分app.js应该看起来像(假设您正在使用expressjs和node.js):
app.js:
当您将某些内容发送到
localhost:8080/test
时,使用此节点配置,您将在订阅回调中收到myTestVar
作为响应 .