【发布时间】:2018-12-30 03:51:45
【问题描述】:
尝试在 Goodreads API 上搜索图书信息。同时我正在尝试将数据从 xml 转换为 json。搜索书籍时出现此错误
请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:3000' 因此不允许访问。
和警告
跨域读取阻塞 (CORB) 阻止了 MIME 类型 application/xml 的跨域响应 https://www.goodreads.com/search/index.xml?key=PL6saHk8cIrLeeLG3eylg&q=halo。详情请见https://www.chromestatus.com/feature/5629709824032768。
我安装了 xml2js 包并像这样将它放入我的函数中
searchBooks = async (e) => {
e.preventDefault();
const search = this.state.search;
try {
let res = await axios.get(
`https://www.goodreads.com/search/index.xml?key=PL6saHk8cIrLeeLG3eylg&q=${search}`
);
let xml = res.data;
parseString(xml, (error, res) => {
this.setState({
books: res.data
});
});
} catch (error) {
this.setState({ error });
}
console.log(this.state.books);
};
我需要解决什么问题?
【问题讨论】:
标签: reactjs xml-to-json