javascript - Angular 5 - HttpClient XML Post - 解析期间 Http 失败

标签 javascript xml angular typescript

我正在尝试使用 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'

这里有什么问题吗?我该如何解决这个问题?

最佳答案

尝试一下

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

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

关于javascript - Angular 5 - HttpClient XML Post - 解析期间 Http 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50231889/

相关文章:

javascript - 效率: Objects inside arrays or arrays inside objects?

asp.net - 配置文件 XDT 转换 InsertIfMissing 正在跳过子项

javascript - 我的 arrayPizzas 有问题,它永远不会进入第一个 if,问题是什么?我想为重复的列表项添加一个 count++

Java : Working with multiple versions of XML Schemas

java从XML文件读取JDBC连接

从 typescript 调用 Javascript 原型(prototype)函数

angular - rxjs处理错误与拦截器不兼容

javascript - 动态组件中的 v 模型

javascript - 为什么检查 runtime.lastError 会生成端口错误?

javascript - 将图像从 Base64 解码为 jpg