【发布时间】:2018-12-31 19:01:20
【问题描述】:
在学习教程时,我在尝试将数据发布到 api 时遇到了一个错误。
我得到的错误:
加载资源失败:服务器响应状态为 422 (Unprocessable Entity)
service.js文件的代码
import 'rxjs/add/operator/toPromise';
import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {CONFIG} from './../config/config';
import { RequestOptions, Headers, } from '@angular/http';
import { headersToString } from '../../../node_modules/@types/selenium-webdriver/http';
@Injectable()
export class AuthService {
private _options = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
constructor(private http: HttpClient) {}
register(name: string, email: string, password: string) {
const headers: Headers = new Headers({ 'Content-Type': 'application/json' });
return this.http.post(`${CONFIG.API_URL}/register`, { name: name, email: email, password: password}, this._options)
.toPromise()
.then((response) => {
console.log(response);
});
}
}
我无法找到此问题的任何解决方案,因此我们将不胜感激。
【问题讨论】:
-
检查网络面板是否有违规请求。将有一个(至少一个)请求失败。分析请求并仔细检查一切是否正常。
-
感谢您的回复@phuzi,我检查了网络面板,发现有一个请求失败,在检查它的响应后,我收到的错误消息是 {"email":[ “选定的电子邮件无效。”]} 但我提供了正确的电子邮件 ID..我仍然很困惑是什么导致了错误..
标签: javascript angular api