我试图将localhost:4200上的Angular4数据发布到localhost:8000上的API.我和Postman合作得很好,但是和Angular不一样.然后我得到:
无法加载资源:服务器响应状态为422(不可处理的实体)
这是发布到api的服务:
@Injectable() export class ApiService { constructor(private http: Http) { } login(username: string,password: string): Observable<Response>{ const url = 'http://localhost:8000/login'; const json = JSON.stringify({username: username,password: password}); const headers: Headers = new Headers(); headers.append('Content-Type','application/json; charset=UTF-8'); return this.http.post(url,json,headers ) .map(res => res.json()); } }
logIn(user: string,password: string){ this.apiService.login(user,password).subscribe( data => console.log(data),error => console.log(error) ); }
解决方法
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate),and the
Syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions.