任何想法如何"翻译"以下php代码示例到NodeJS中的HTTP API请求中?
describe('MyComponent propTypes validation', () => {
it('validates propA', () => {
expect(MyComponent.propTypes.propA).toBe(PropTypes.string.isRequired);
})
// THIS FAILS. Expected: [Function bound checkType], actual: [Function bound checkType]
it('validates propB', () => {
expect(MyComponent.propTypes.propB)
.toEqual(
PropTypes.shape({
inner1: PropTypes.bool.isRequired,
inner2: PropTypes.func,
}).isRequired
);
})
})
params必须以(auth,params,XML)POST变量发送。
提前很多,
亲切的问候, 泽索特
答案 0 :(得分:0)
在浏览器中,您可以使用jQuery轻松地执行请求和解析XML:
$.ajax({
url: "https://mail.google.com/mail/feed/atom/",
dataType: "xml",
success: function(data) {
console.log(data);
}
});
或者,如果您只想解析XML,可以使用DOMParser():
parser = new DOMParser();
xmlDoc = parser.parseFromString(txt, "text/xml");
使用 Node.js ,它有点复杂。 Javascript熟悉JSON(JavaScript Object Notation),因此XML解析器中没有构建。您必须要求某种XML解析器模块,例如:
完整示例:
var fetch = require('node-fetch'), //implements Fetch API for making requests
libxmljs = require("libxmljs"); //xml parser, read about it here: https://github.com/libxmljs/libxmljs
fetch('http://SOME_PAGE.com/data?type=xml')
.then((res) => res.text())
.then((xml) => {
var xmlDoc = libxmljs.parseXml(xml);
//do something with xmlDoc
});