javascript - Angular 5 中的 422(无法处理的实体)错误

标签 javascript angular api

在学习教程时,我在尝试将数据发布到 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/

相关文章:

带有变量的 Angular2 routerLink 片段

mysql - express.js 中的 API 删除方法错误

api - 如何使用YouTube API V3

c# - 如何更新 GridView 页脚模板中标签的值?

javascript - 为什么 React 会以不变的状态一遍又一遍地渲染我的组件?

javascript - 使用 Jquery 将 div 内容单独的行和单元格导出到 Excel?

angular - 动态按下 Enter 时禁用输入

javascript - 如何确定状态何时完成更新?

ios - Angularfire2 FirebaseListObservable 在 iOS 上加载缓慢

api - Youtube API 每天最大视频上传次数