首页 > 解决方案 > Angular 5 - HttpClient XML Post - 解析期间的 Http 失败

问题描述

我正在尝试使用 xml 登录本地 Web 服务:

这是代码:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'text/xml',
    'Accept':  'text/xml',
    'Response-Type': 'text'
  })
};


login() {
    const postedData = `
        <authenticationDetail>
            <userName>myusername</userName>
            <password>mypassword</password>
        </authenticationDetail>`;
    return this.http.post('http://localhost/login.ws', postedData, httpOptions)
    .subscribe(
        result => {
            console.log('This result is' + result);
        },
        error => {
            console.log('There was an error: ', error);
        }
    );
}

我得到的错误是:

Http failure during parsing for 'http://localhost/login.ws'

这里有什么问题?我怎样才能解决这个问题?

标签: javascriptxmlangulartypescript

解决方案


你试过了吗

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'text/xml',
        'Accept':  'text/xml',
        'Response-Type': 'text'
    }), 
    responseType: 'text'
};

responseType 必须设置为text不仅在 headers 中,而且在httpOptions. 否则 Angular 会将调用的响应解析为 JSON。


推荐阅读