此代码取消GET请求,但无法中止POST调用
如果我首先发送GET请求并且我不通过abortAll
方法取消它们,他们只是自己完成这个令牌自己取消并且对下一个请求不起作用?
我错过了什么?
谢谢,约翰
import axios from 'axios'
class RequestHandler {
constructor(){
this.cancelToken = axios.CancelToken;
this.source = this.cancelToken.source();
}
get(url,callback){
axios.get(url,{
cancelToken:this.source.token,
}).then(function(response){
callback(response.data);
}).catch(function(err){
console.log(err);
})
}
post(url,callbackOnSuccess,callbackOnFail){
axios.post(url,{
cancelToken:this.source.token,
}).then(function(response){
callbackOnSuccess(response.data);
}).catch(function(err){
callbackOnFail()
})
}
abortAll(){
this.source.cancel();
// regenerate cancelToken
this.source = this.cancelToken.source();
}
}
答案 0 :(得分:6)
我发现你可以通过这种方式取消发帖请求,我很想念this documentation part。 在之前的代码中,我已将cancelToken传递给POST数据请求,而不是作为axios设置。
import axios from 'axios'
var CancelToken = axios.CancelToken;
var cancel;
axios({
method: 'post',
url: '/test',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
},
cancelToken: new CancelToken(function executor(c) {
// An executor function receives a cancel function as a parameter
cancel = c;
})
}).then(()=>console.log('success')).catch(function(err){
if(axios.isCancel(err)){
console.log('im canceled');
}
else{
console.log('im server response error');
}
});
// this cancel the request
cancel()
答案 1 :(得分:0)
也许我错了,但是每个请求都应该有一个唯一的源对象。
答案 2 :(得分:0)
使用cancelToken和源对新请求取消以前的Axios请求。
https://github.com/axios/axios#cancellation
// cancelToken and source declaration
const CancelToken = axios.CancelToken;
let source = CancelToken.source();
source && source.cancel('Operation canceled due to new request.');
// save the new request for cancellation
source = axios.CancelToken.source();
axios.post(url, postData, {
cancelToken: source.token
})
.then((response)=>{
return response && response.data.payload);
})
.catch((error)=>{
return error;
});
答案 3 :(得分:0)
在componentDidMount生命周期挂钩内部使用:
useEffect(() => {
const ourRequest = Axios.CancelToken.source() // <-- 1st step
const fetchPost = async () => {
try {
const response = await Axios.get(`endpointURL`, {
cancelToken: ourRequest.token, // <-- 2nd step
})
} catch (err) {
console.log('There was a problem or request was cancelled.')
}
}
fetchPost()
return () => {
ourRequest.cancel() // <-- 3rd step
}
}, [])
注意:对于POST请求,请将cancelToken作为第三个参数传递
Axios.post(`endpointURL`, {data}, {
cancelToken: ourRequest.token, // 2nd step
})
答案 4 :(得分:0)
使用ReactJs的最简单实现
import axios from 'axios';
class MyComponent extends Component {
constructor (props) {
super(props)
this.state = {
data: []
}
}
componentDidMount () {
this.axiosCancelSource = axios.CancelToken.source()
axios
.get('data.json', { cancelToken: this.axiosCancelSource.token })
.then(response => {
this.setState({
data: response.data.posts
})
})
.catch(err => console.log(err))
}
componentWillUnmount () {
console.log('unmount component')
this.axiosCancelSource.cancel('Component unmounted.')
}
render () {
const { data } = this.state
return (
<div>
{data.items.map((item, i) => {
return <div>{item.name}</div>
})}
</div>
)
}
}