Reactjs 在React Select Async上获取网络错误和空下拉列表
我正在尝试在我的react应用程序上实现react select。我的API端点与一个简单的get请求一起工作,我确信它会发送如下响应:Reactjs 在React Select Async上获取网络错误和空下拉列表,reactjs,axios,react-select,Reactjs,Axios,React Select,我正在尝试在我的react应用程序上实现react select。我的API端点与一个简单的get请求一起工作,我确信它会发送如下响应: [ { value: 123, label: "Michael Carrick", first_name: "Michael", last_name: "Carrick", country: "England", first_nationality: "England", team_id: 134,
[
{
value: 123,
label: "Michael Carrick",
first_name: "Michael",
last_name: "Carrick",
country: "England",
first_nationality: "England",
team_id: 134,
Team.name: "Manchester United"
},
{
value: 234,
label: "Jack Clarke",
first_name: "Jack",
last_name: "Clarke",
country: "England",
first_nationality: null,
team_id: 254,
Team.name: "Leeds United"
}
]
在我的组件中,我有以下部分:
const promiseOptions = (inputValue) => {
axios.get('URL?q=' + inputValue)
.then(res => {
const data = res.data;
return data;
})
.catch((error) => {
console.log(error);
});
}
export default class extends Component {
constructor(props) {
super(props);
this.state = {
data: null,
inputValue: ''
};
}
componentDidMount() {
axios.get('URL')
.then(res => {
const data = res.data;
this.setState({ data });
return data;
})
.catch((error) => {
console.log(error);
});
}
handleInputChange (newValue) => {
this.setState({ inputValue });
return inputValue;
};
render() {
return (
<AsyncSelect cacheOptions defaultOptions loadOptions={promiseOptions} />
)
}
}
我试图尽可能地遵循示例,但似乎无法使其正常工作
编辑:我的Express应用程序上允许使用CORS。尝试使用此代码,您需要使用回调:
const promiseOptions=(inputValue,回调)=>{//add callback param
get('URL?q='+inputValue)
。然后(res=>{
常数数据=分辨率数据;
回调(数据);//{
console.log(错误);
});
}
使用回调时,我得到无法加载资源:net::ERR_BLOCKED_BY_CLIENT
error。你在哪里看到错误?在控制台中?你有广告拦截器吗?是的,但即使我关闭它也没关系。但这是一个网络错误,不是react,不是JS…你需要关闭它,不是为了当前页面,而是为了“URL”在另一个浏览器中,域名可能是您正在使用的另一个插件/插件…但是使用promise的方式是回调
VM6467:1 GET ENDPOINT-URL?q=c 0 ()
(anonymous) @ VM6467:1
dispatchXhrRequest @ xhr.js:178
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:59
Promise.then (async)
request @ Axios.js:51
Axios.(anonymous function) @ Axios.js:61
wrap @ bind.js:9
promiseOptions @ select.js:14
loadOptions @ Async.js:167
(anonymous) @ Async.js:88
callCallback @ react-dom.development.js:10764
commitUpdateQueue @ react-dom.development.js:10797
commitLifeCycles @ react-dom.development.js:14264
commitAllLifeCycles @ react-dom.development.js:15342
callCallback @ react-dom.development.js:100
invokeGuardedCallbackDev @ react-dom.development.js:138
invokeGuardedCallback @ react-dom.development.js:187
commitRoot @ react-dom.development.js:15481
completeRoot @ react-dom.development.js:16496
performWorkOnRoot @ react-dom.development.js:16440
performWork @ react-dom.development.js:16358
flushInteractiveUpdates$1 @ react-dom.development.js:16605
batchedUpdates @ react-dom.development.js:2143
dispatchEvent @ react-dom.development.js:4551
interactiveUpdates$1 @ react-dom.development.js:16592
interactiveUpdates @ react-dom.development.js:2150
dispatchInteractiveEvent @ react-dom.development.js:4528
select.js:20 Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)