当我在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;
}

答案 0 :(得分:0)
将您的组件更改为
var auth:Auth[] = [
{login:'...',password:'..' } //put it outside class
]
ngOnInit() {
this.httpService.postData(auth).subscribe((data) => this.res);
}