我正在尝试使用来自 Angular的HttpClient从SOAP API检索 xml
这是我的app.component.html上的内容:
<input type="submit" value="test POST" (click)="onClickMe()"/>
这是在我的app.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
results: string[];
constructor(private http: HttpClient) {
console.log('constructor opened');
}
ngOnInit(): void{
console.log('onInit opened');
}
onClickMe(){
var emp = '<<credentials to the webservice>>';
var login = '<<credentials to the webservice>>';
var pass = '<<credentials to the webservice>>';
var id = "<<credentials to the webservice>>";
const body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n <soap12:Body>\r\n <Lista xmlns=\"http://GSMMX.WebService/\">\r\n <Emp>"+emp+"</Emp>\r\n <Login>"+ login +"</Login>\r\n <Senha>"+ pass +"</Senha>\r\n <id>"+ id +"</id>\r\n </Lista>\r\n </soap12:Body>\r\n</soap12:Envelope>";
const url = "<<webserviceURL>>";
console.log('onClickme opened');
this.http.post(url, body, {
headers: new HttpHeaders()
.set('Content-Type', 'text/xml')
.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS')
.append('Access-Control-Allow-Origin', '*')
.append('Access-Control-Allow-Headers', "Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Request-Method")
}).subscribe(data => {
console.log(data);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
}
}
);
}
}
所以,我使用 Postman 做了同样的请求,并复制了 url 和 body,就像成功的 POST 一样。我尝试使用 Angular 5 来做到这一点,但它不起作用,它返回以下消息:
Backend returned code 200, body was: [object Object]
有人知道我该怎么做吗?该请求得到一个 200 代码,但响应没有以正确的方式出现。
我很感激任何帮助!