Angular5不起作用的api

时间:2018-07-17 06:20:43

标签: angular validation post

enter image description here,当我调用post API时,出现如下错误,如附件图像所示。 但是当我使用邮递员时,该API对我有效。这是Angular4项目的一部分。

数据结构就是这样。

datastructure = {
    name: '',
    phone: '',
    password: '',
    company: '',
    email: {},
    projects: [
      {
        projectName: '',
        startTime: '',
        isNewProject: false,
        developers: [],
        externalTeam: {
          email: {}
        },
        typeOfProject: {
          mobile: {
            android: false,
            ios: false,
            hybrid: false
          },
          web: false,
          iot: false,
          helpers: {
            projectManager: false,
            designer: false,
            developer: {
              skills: [],
            }
          },
        }
      }
    ]
  };

调用api是这个。

const clientUrl = 'https://recruiting.pimentagroup.de/pimenta/api/client';

数据结构正在从输入框中获取信息。

this.datastructure.projects[0].projectName = this.accountForm.value['project'];
    this.datastructure.name = this.accountForm.value['name'];
    this.datastructure.email = this.accountForm.value['email'];
    this.datastructure.company = this.accountForm.value['company'];
    this.datastructure.password = encrypted.toString();
    this.datastructure.phone = this.accountForm.value['phone'];
    this.http.post(clientUrl, this.datastructure, { observe: 'body', responseType: 'json' })
      .subscribe(
        data => console.log(data)
      );

initForms:

constructor(private http: HttpClient, private auth: AuthService, private router: Router) { }
  private initForm() {
    const project = '';
    const name = '';
    const email = '';
    const phone = '';
    const password = '';
    const company = '';
    const nda = false;

    this.accountForm = new FormGroup({
      'project': new FormControl(project, Validators.required),
      'name': new FormControl(name, Validators.required),
      'email': new FormControl(email, [Validators.required,
      // tslint:disable-next-line:max-line-length
      Validators.pattern(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
      ]),
      'phone': new FormControl(phone, Validators.required),
      'password': new FormControl(password, Validators.required),
      'company': new FormControl(company, Validators.required),
      'nda': new FormControl(nda, Validators.required)
    });

  }

运行此项目时,出现enter image description here以下错误。

请让我知道。

服务代码。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
// tslint:disable-next-line:import-blacklist
import { Observable } from 'rxjs';

const TOKEN_KEY = 'pimenta';

@Injectable()
export class AuthService {
    private authUrl = 'https://recruiting.pimentagroup.de/pimenta/api/oauth/';

    constructor(private http: HttpClient) { }

    login(username: string = 'website', password: string = '0be402f4'): Observable<{}> {

        const credentials = {
            username: '',
            password: ''
        };

        credentials.username = username;
        credentials.password = password;

        this.clearToken();

        return this.http.post(this.authUrl, credentials);
    }

    public getToken(): string {
        return localStorage.getItem(TOKEN_KEY);
    }
    public setToken(val: string) {
        localStorage.clear();
        localStorage.setItem(TOKEN_KEY, val);
    }
    public clearToken() {
        localStorage.clear();
    }
}

0 个答案:

没有答案