【发布时间】:2018-03-01 04:12:23
【问题描述】:
我正在尝试将数据从 localhost:4200 上的 Angular4 发布到 localhost:8000 上的 API。我在 Postman 上工作得很好,但在 Angular 上却不行。然后我得到:
加载资源失败:服务器响应状态为 422 (Unprocessable Entity)
这是发布到 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)
);
}
【问题讨论】:
-
为什么要在构造函数中定义方法?
-
我没有。我定义了对象 http。这是一种有角度的做法。
-
对不起,我没有看到 { }
-
你找到解决方案了吗@Skvupp
-
是的,这是使用 Lumen 框架的服务器问题。 Angular 代码是正确的。
标签: angular http-post http-status-code-422