Reactjs 反应效果获取CORS问题
我很难找到关于在这个try/catch块中放置cor头的位置的信息 e、 g:模式:“无cors”Reactjs 反应效果获取CORS问题,reactjs,api,react-hooks,cors,Reactjs,Api,React Hooks,Cors,我很难找到关于在这个try/catch块中放置cor头的位置的信息 e、 g:模式:“无cors” useEffect(() => { const fetchData = async() => { try { setLoading(true); const response = await fetch('http://[::1]:3000/users'); const json = await response.json();
useEffect(() => {
const fetchData = async() => {
try {
setLoading(true);
const response = await fetch('http://[::1]:3000/users');
const json = await response.json();
setData(json.results, setLoading(false));
} catch(err) {
console.warn('API Error:', err);
setLoading(false);
}
}
if(amount) {
fetchData(amount);
}
}, [amount])
我是新来的钩子,并将感谢任何建议或改善与后端的沟通
非常感谢:fetch方法可以选择接受第二个参数,该参数允许您控制许多不同的设置:
useEffect(() => {
const fetchData = async() => {
try {
setLoading(true);
const response = await fetch('http://[::1]:3000/users', {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
const json = await response.json();
setData(json.results, setLoading(false));
} catch(err) {
console.warn('API Error:', err);
setLoading(false);
}
};
if(amount) {
fetchData(amount);
}
}, [amount]);
查看文档了解更多详细信息