我正在尝试使用提取API将XML字符串而不是JSON对象发布到node.js
服务器。
这是我的发布JSON对象的代码:
handleSubmit = async e => {
e.preventDefault();
var request = JSON.stringify({
drug: this.state.drug,
disease: this.state.disease,
type: this.state.type
});
var xmlRequest = js2xmlparser.parse("request", request);
const response = await fetch('/api/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: request
});
const body = await response.text();
this.setState({
responseToPost: body
});
}
如何编辑代码以在请求正文中发布XML字符串(xmlRequest)而不是JSON(请求)。
发送xmlRequest
而不是正文中的request
。同样将Content-Type
更改为text/xml
或application/xml
const request = {
drug: this.state.drug,
disease: this.state.disease,
type: this.state.type
};
const xmlRequest = js2xmlparser.parse('request', request);
const response = await fetch('/api/submit', {
method: 'POST',
headers: {
'Content-Type': 'text/xml'
},
body: xmlRequest
});
[js2xmlparser
接受一个对象作为第二个参数,请勿使用JSON.stringify
。