Угловой почтовый запрос заканчивается на 400

Когда я отправляю тот же почтовый запрос в Postman, все в порядке. Но с Angular я получил 400 Bad Request. Пожалуйста, помогите мне понять, в чем проблема, потому что у меня нет идей.

http.service.ts

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Response, Headers} from '@angular/http';
import {Auth} from './auth';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
 
@Injectable()
export class HttpService{
 
    constructor(private http: Http){ }
     
    postData(obj: Auth){
        const body = JSON.stringify(obj);
         
        let headers = new Headers({ 'Content-Type': 'application/json;' });
         
        return this.http.post('https://integrationapi.net/rest/user/sessionid', body, { headers: headers })
                        .map((resp:Response)=>resp.json())
                        .catch((error:any) =>{return Observable.throw(error);}); 
    }
}

app.component.ts

import { Component, OnInit } from '@angular/core';
import {Auth} from './auth';
import { HttpService} from './http.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [HttpService],
})
export class AppComponent {
  auth: Auth=new Auth();



  res : string = '';

  constructor(private httpService: HttpService){}

    ngOnInit() {

    	this.auth.login = '********';
  		this.auth.password = '********';

  		console.log(this.auth);

      	this.httpService.postData(this.auth).subscribe((data) => this.res);
    }
}

auth.ts

export class Auth {
	login    : string;
	password : string;
}


person Qwerty    schedule 01.06.2017    source источник
comment
400 означает, что запрос был неправильно сформирован. Другими словами, поток данных, отправляемый клиентом на сервер, не соответствовал правилам. И в вашем коде вам не нужны заголовки, потому что заголовок по умолчанию для ввода и публикации в angular в application/json, попробуйте удалить его.   -  person Akashii    schedule 01.06.2017
comment
а также, вам не нужно new Auth(). авторизация: авторизации достаточно   -  person Akashii    schedule 01.06.2017
comment
и измените class Auth на interface Auth   -  person Akashii    schedule 01.06.2017
comment
Спасибо. Но после изменений с Auth для интерфейса и «auth: Auth» у меня появилась другая ошибка i.piccy.info/i9/3f58503ac9dad45c19860f6475e05b32/1496304056/   -  person Qwerty    schedule 01.06.2017


Ответы (1)


Измените свой компонент на

   var auth:Auth[] = [
    {login:'...',password:'..' }  //put it outside class
    ]


 ngOnInit() {

        this.httpService.postData(auth).subscribe((data) => this.res);
    }
person Akashii    schedule 01.06.2017