Node.js Angular 6未从expressjs节点服务器获得响应
帮助一个noob,我正在构建一个meanstack应用程序,我遇到了一个问题,我无法从express服务器读取响应,但响应是在我使用postman时生成的,下面是我的代码 auth.service.tsNode.js Angular 6未从expressjs节点服务器获得响应,node.js,angular,Node.js,Angular,帮助一个noob,我正在构建一个meanstack应用程序,我遇到了一个问题,我无法从express服务器读取响应,但响应是在我使用postman时生成的,下面是我的代码 auth.service.ts import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { map } from 'rxjs/operators'; @Injectable({ pr
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthService {
authToken: any;
user: any;
constructor(private http:Http) { }
registerUser(user){
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:3000/users/register',user,
{headers: headers})
.pipe(map(res => res.json));
}
authenticateUser(user){
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:3000/users/authenticate',user,
{headers: headers})
.pipe(map(res => res.json));
}
}
login.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';
import { FlashMessagesService } from 'angular2-flash-messages';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
username: String;
password: String;
constructor(private authService: AuthService,
private router: Router,
private flashMessage: FlashMessagesService
) { }
ngOnInit() {
}
onLoginSubmit(){
const user = {
username: this.username,
password: this.password
}
this.authService.authenticateUser(user).subscribe(data => {
console.log(data);
});
}
}
镀铬控制台
ƒ () { login.component.ts:29
if (typeof this._body === 'string') {
return JSON.parse(this._body);
}
if (this._body instanceof ArrayBuffer) {
return JSON.parse(this.text());
以下是《邮差》杂志的回应:
看起来像是来自
认证者的数据
是一个函数。你试过调用它吗?错误在你的管道中
pipe(map( res => res.json ))
您需要在地图中调用res.json()
。将其转换为
pipe(map( res => res.json() ))
但是,在AngularV5上不需要将响应转换为JSON
正确代码如下:-
authenticateUser(user){
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:3000/users/authenticate',user,
{headers: headers})
.pipe(map(res => res.json()));
}
如果您签入浏览器开发工具,是否可以在“网络”选项卡中看到请求?欢迎使用StackOverflow!请删除angularjs
标记,因为它是针对较旧的angularjs 1.xYes的,我在login.component中调用了它