【发布时间】:2019-09-24 07:28:14
【问题描述】:
我有一个带按钮的搜索栏。单击按钮时,search_on_click 函数运行。该函数应该检索 URL 的 html 并将其显示在页面上。
class App extends Component{
constructor(props){
super(props)
this.state = {
html: []
}
this.search = this.search.bind(this)
}
search(){
const URL = 'https://www.zerochan.net/Re%3AZero+Kara+Hajimeru+Isekai+Seikatsu?s=fav'
axios.get(URL)
.then(data =>{
console.log(data)
this.setState({
query: data
})
})
.catch(function(error){
console.log(error);
})
}
render(){
return(
<div>
<button onClick={() => this.search()}>search</button>
<div>{this.state.query}</div>
</div>
这是我到目前为止的代码。问题/疑问是:
axios 不会 console.log html 甚至似乎运行
我已经尝试过 fetch/requests,但问题或多或少是一样的
- 有更好的方法吗?
- 我认为这不是 CORS 问题,因为我使用了允许 chrome 扩展的 CORS。
- .catch() 也不向控制台记录任何内容
感谢您的宝贵时间。
【问题讨论】:
-
“我不认为这是一个 CORS 问题” 为什么不呢? (顺便说一句,CORS 是一个解决方案,不是问题。问题在于SOP。)
-
你调试这个发现没有发送请求(通过调试工具的“网络”选项卡?),还是有错误响应?
-
Axios Example 有一个
.catch来处理错误情况,您应该使用它。 -
在网络选项卡中,当我单击应该激活 axios 搜索的按钮时,有一个简短的红色 URL 链接会迅速消失
-
.then仅在成功时发生。您应该添加.catch或以其他方式处理错误。这就是为什么“结果”永远不会出现的原因。
标签: javascript reactjs request axios fetch