在 reactjs 中使用 const 发送获取参数
send fetch params using a const in reactjs
我正在执行这样的 GET 获取请求:
let [responseData, setResponseData] = React.useState('');
const dispatch = useDispatch();
const fetchData = React.useCallback(() => {
let headers = new Headers({
'Content-Type': 'application/json',
});
fetch('http://localhost:3000/current_user', {
method: 'GET',
mode: 'cors',
headers: headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
})
.then(response => {
if (response.ok) return response.json();
throw new Error('Request failed.');
})
.then(data => {
setResponseData(data); // sent user data to redux
dispatch(props.setUser(data));
})
.catch(error => {
console.log(error);
});
}, []);
React.useEffect(() => {
fetchData();
}, [fetchData]);
但我想将所有获取请求参数放在一个 const 中,并在获取函数中调用该 const,如下所示:
let [responseData, setResponseData] = React.useState('');
const dispatch = useDispatch();
const fetchData = React.useCallback(() => {
let headers = new Headers({
'Content-Type': 'application/json',
});
const reqParams = {
method: 'GET',
mode: 'cors',
headers: headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
}
fetch('http://localhost:3000/current_user', {
reqParams,
})
.then(response => {
if (response.ok) return response.json();
throw new Error('Request failed.');
})
.then(data => {
setResponseData(data); // sent user data to redux
dispatch(props.setUser(data));
})
.catch(error => {
console.log(error);
});
}, []);
React.useEffect(() => {
fetchData();
}, [fetchData]);
但这并不是成功的提取调用。我做错了什么,我无法弄清楚这里的错误。我怎样才能正确地编写那个函数。
它不起作用,因为参数嵌套在 reqParams
键下。
应该是
fetch('http://localhost:3000/current_user', reqParams)
注意
fetch('http://localhost:3000/current_user', {
reqParams,
})
与
相同
fetch('http://localhost:3000/current_user', {
reqParams: {
method: 'GET',
mode: 'cors',
headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
}
})
我正在执行这样的 GET 获取请求:
let [responseData, setResponseData] = React.useState('');
const dispatch = useDispatch();
const fetchData = React.useCallback(() => {
let headers = new Headers({
'Content-Type': 'application/json',
});
fetch('http://localhost:3000/current_user', {
method: 'GET',
mode: 'cors',
headers: headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
})
.then(response => {
if (response.ok) return response.json();
throw new Error('Request failed.');
})
.then(data => {
setResponseData(data); // sent user data to redux
dispatch(props.setUser(data));
})
.catch(error => {
console.log(error);
});
}, []);
React.useEffect(() => {
fetchData();
}, [fetchData]);
但我想将所有获取请求参数放在一个 const 中,并在获取函数中调用该 const,如下所示:
let [responseData, setResponseData] = React.useState('');
const dispatch = useDispatch();
const fetchData = React.useCallback(() => {
let headers = new Headers({
'Content-Type': 'application/json',
});
const reqParams = {
method: 'GET',
mode: 'cors',
headers: headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
}
fetch('http://localhost:3000/current_user', {
reqParams,
})
.then(response => {
if (response.ok) return response.json();
throw new Error('Request failed.');
})
.then(data => {
setResponseData(data); // sent user data to redux
dispatch(props.setUser(data));
})
.catch(error => {
console.log(error);
});
}, []);
React.useEffect(() => {
fetchData();
}, [fetchData]);
但这并不是成功的提取调用。我做错了什么,我无法弄清楚这里的错误。我怎样才能正确地编写那个函数。
它不起作用,因为参数嵌套在 reqParams
键下。
应该是
fetch('http://localhost:3000/current_user', reqParams)
注意
fetch('http://localhost:3000/current_user', {
reqParams,
})
与
相同fetch('http://localhost:3000/current_user', {
reqParams: {
method: 'GET',
mode: 'cors',
headers,
cache: 'no-cache',
redirect: 'follow',
referrer: 'no-referrer',
credentials: 'include',
}
})