我试图将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.
例如,如果XML请求主体包含格式正确(即语法正确)但语义错误的XML指令,则可能发生此错误情况.