javascript - 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'
这里有什么问题?我怎样才能解决这个问题?
解决方案
你试过了吗
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'text/xml',
'Accept': 'text/xml',
'Response-Type': 'text'
}),
responseType: 'text'
};
responseType 必须设置为text
不仅在 headers 中,而且在httpOptions
. 否则 Angular 会将调用的响应解析为 JSON。
推荐阅读
- vue.js - 更新状态的最佳方式
- r - 如何在左右拆分箱线图和抖动图
- python - 将 Mongodb 时间戳转换为日期
- jquery - 如何在水平幻灯片动画中显示超过 5 个图像?
- vue.js - Nuxt Axios 访问 XMLHttpRequest 错误 - 不从源调用 .NET webAPi
- graphql - 激活ssr时withUrqlClient不起作用?
- python - Python按某些父标签跟踪某些属性计数组
- c# - 在 MacOS、.Net Core 3.1 / .Net 5.0 上看不到 C# 中的系统环境变量。(视觉工作室)
- python - tkinter 和枕头:如何使坐标和颜色依赖于用户输入?
- html - 如何使元素之间的线条自适应?