javascript - 将子字符串应用于 axios 响应
问题描述
我需要在我的 axios 响应中获取一些字符串之间的文本,我有这种语法但不起作用,即使我尝试只将 response.data 值传递给 res 属性?并在控制台中打印结果,但我只收到一条未定义的消息。
提前谢谢你们。
<script>
import Axios from 'axios';
export default {
data() {
res: "",
report: ""
},
methods: {
sendTest() {
Axios.post('http://server/',JSON.parse(Payload))
.then(response => {
this.res = response.data
this.report = this.substr(
this.res.indexOf('<TAG1>') + 1,
this.res.indexOf('</TAG1>')
)
console.log(this.report);
})
.catch(e => {
console.log(e.response)
});
},
}
}
</script>
解决方案
首先你应该检查是否response.data
是一个字符串。如果是字符串,您必须解析您的 xml 数据。为此,您可以:
parser = new DOMParser();
xmlDoc = parser.parseFromString(response.data,"text/xml");
this.res = xmlDoc.getElementByTagName("tag")[0].textContent
但是你必须关心旧的 IE 版本。他们不支持 DOMParser。对于旧版浏览器,您必须添加:
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(text);
}
this.res = xmlDoc.getElementByTagName("tag")[0].textContent
如果您的 response.data 已经是已解析的 xml,您只需执行以下操作:
this.res = response.data.getElementByTag("tag")[0].textContent
推荐阅读
- javascript - 从后端代码向 bootstrap-notify 发送数据
- mysql - 如何使用 ActiveRecord 进行两个连接并执行单个活动记录查询?
- regex - 如何sed或perl用不带引号的点和斜杠替换文本?
- c++ - 如何使用级联阴影映射制作软阴影?
- typescript - 在带有 Typescript 的 React Native StyleSheet 中使用函数
- oracle - 编译过程时没有足够的值
- sql - 我也不明白 ORA-01779 指的是什么列
- python - Python Plotnine - 创建堆积条形图
- sql-server - 优化单个表上具有多个连接的查询
- html - 选择 html 标记中任何位置具有文本徽标的 img 标记