【问题标题】:422 (Unprocessable Entity) error in Angular 5Angular 5 中的 422(无法处理的实体)错误
【发布时间】: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


【解决方案1】:

似乎更多的是服务器端问题而不是客户端代码问题。

符合 422 的定义:服务器理解请求实体的内容类型(因此 415 Unsupported Media Type 状态码不合适),并且 请求实体的语法是正确的(因此 400 Bad Request 状态 代码不合适)但无法处理包含的 说明。

检查网络面板以验证是否传递了格式正确的 JSON 并设置了正确的标头。

尝试通过检查异常和调试流程来调试服务器端的问题。

还显示生成的 JSON 并传递给服务器以获取确切的问题并提及后端框架。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2023-03-22
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2022-10-15
    相关资源
    最近更新 更多