core.es5.js:1020 ERROR TypeError:error.json不是函数 在CatchSubscriber.webpackJsonp ... / .. / .. / .. / .. / src / app / user.service.ts.UserService.handelError [作为选择器](user.service.ts:58) 在CatchSubscriber.webpackJsonp ... / .. / .. / .. / rxjs / operators / catchError.js.CatchSubscriber.error (catchError.js:104) 在MapSubscriber.webpackJsonp ... / .. / .. / .. / rxjs / operators / map.js.MapSubscriber._next (map.js:82) 在MapSubscriber.webpackJsonp ... / .. / .. / .. / rxjs / Subscriber.js.Subscriber.next (Subscriber.js:90) 在XMLHttpRequest.onLoad(http.es5.js:1226) 在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invokeTask (zone.js:425) at Object.onInvokeTask(core.es5.js:3881) 在ZoneDelegate.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneDelegate.invokeTask (zone.js:424) 在Zone.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.Zone.runTask(zone.js:192) 在ZoneTask.webpackJsonp ... / .. / .. / .. / zone.js / dist / zone.js.ZoneTask.invokeTask [作为调用]
当我想添加表格时,我有这个错误可以有人知道原因。
add.components.ts
import { Component, OnInit } from '@angular/core';
import {UserService} from '../user.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-add-user',
templateUrl: './add-user.component.html',
styleUrls: ['./add-user.component.css']
})
export class AddUserComponent implements OnInit {
username: string ;
email: string;
errors= [];
constructor(private _userService: UserService , private router: Router) { }
addUser(username, email) {
let user: any;
user = {username: username, email: email};
this._userService.addUser(user).subscribe(
result => this.router.navigate(['/users']),
err => this.errors.push(err)
);
}
ngOnInit() {
}
}
表单add.html
<form (submit)="addUser(username,email)" class="well">
<div class="form-group">
<label>username:</label>
<input type="text" class="form-control" placeholder="add username" [(ngModel)]="username" name="username" >
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" placeholder="add email" [(ngModel)]="email" name="email" >
</div>
<div *ngFor="let error of errors" class="alert alert-danger">
<div>There is an error in :{{error.field}} field</div>
<div>{{error.message}}</div>
</div>
<button type="submit" class="btn btn-primary" >Save</button>
</form>
User.ts
import { Injectable } from '@angular/core';
import {Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import {User} from './User';
import {AuthService} from './auth.service';
@Injectable()
export class UserService {
private uri= 'http://127.0.0.1:8000/api/users';
constructor(private http: Http, private authenticationService: AuthService ) {}
getUsers(): Observable<any[]> {
const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
return this.http.get(this.uri , {headers : headers}).map(res => <User[]> res.json() )
.catch(this.handelError);
}
addUser(user: User) {
const headers = new Headers();
headers.append('content-type', 'application/json');
headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
return this.http.post(this.uri, JSON.stringify(user), {headers : headers})
.map(res => res.json()).catch(this.handelError);
}