在学习教程时,我在尝试将数据发布到 api 时遇到错误。
我得到的错误:
无法加载资源:服务器响应状态为 422(无法处理的实体)
服务代码.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);
});
}
}
我无法找到此问题的任何解决方案,因此我们将不胜感激。
最佳答案
似乎更多的是服务器端问题而不是客户端代码问题。
Acc to defination of 422 : 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.
检查网络面板以验证是否传递了格式正确的 JSON 并设置了正确的 header 。
尝试通过检查异常和调试流程在服务器端调试问题。
同时显示生成的 JSON,您将传递给服务器以获得确切的问题并提及后端框架。
关于javascript - Angular 5 中的 422(无法处理的实体)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493818/