问题描述
我可以在我的 react.js 项目中从 https://api.duckduckgo.com/
获取 DuckDuckGo 的搜索结果。但由于它给我们仅即时答案结果,我无法获得完整的搜索结果。
import React,{ Component } from 'react';
import { render } from 'react-dom';
import axios from 'axios';
interface AppProps { }
interface AppState {
name: string;
}
class App extends Component<AppProps,AppState> {
constructor(props) {
super(props);
this.state = {
name: 'React',query: '',img: "",};
}
search = () => {
const options = {
method: 'GET',url: 'https://api.duckduckgo.com/',params: {
q: this.state.query,no_redirect: '1',no_html: '1',skip_disambig: '1',format: 'json'
},};
axios.request(options).then((res) => {
const b = res.data; // Search result is not satisfied for me
if(b.Abstract != "") {
this.setState({
img: b.Image
})
}
}).catch(function (error) {
console.error(error);
});
}
render() {
return (
<div>
<Hello name={this.state.name} />
<p>
Start editing to see some magic happen :)
</p>
<input type="text" onChange={(e) => this.setState({query: e.target.value})} />
<button type="button" onClick={this.search.bind(this)} >Search </button>
{ this.state.img !== "" && (<img src={`https://duckduckgo.com${this.state.img}`} alt="" />) }
</div>
);
}
}
render(<App />,document.getElementById('root'));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)