From c51054f24d49ecf76c6249c9e7a2d37a7d01376a Mon Sep 17 00:00:00 2001 From: Nero Date: Mon, 22 Jan 2018 13:00:21 +0000 Subject: [PATCH 1/2] add example of cancelling a POST request to readme it seems when cancelling a post request we need to specify cancellation token as an axios parameter and not as a data item. --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 95d91e7..ac532ad 100644 --- a/README.md +++ b/README.md @@ -525,6 +525,29 @@ axios.get('/user/12345', { source.cancel('Operation canceled by the user.'); ``` +```js +var CancelToken = axios.CancelToken; +var source = CancelToken.source(); + +axios( + method: 'POST', + url: '/user/12345', + data: { + name: 'new name' + }, + cancelToken: source.token +}).catch(function(thrown) { + if (axios.isCancel(thrown)) { + console.log('Request canceled', thrown.message); + } else { + // handle error + } +}); + +// cancel the request (the message parameter is optional) +source.cancel('Operation canceled by the user.'); +``` + You can also create a cancel token by passing an executor function to the `CancelToken` constructor: ```js From a8de2cf485cef106427bd7705ea30ddcd5e6f6ac Mon Sep 17 00:00:00 2001 From: Nero Date: Mon, 22 Jan 2018 16:36:45 +0000 Subject: [PATCH 2/2] update example according to feedback from PR --- README.md | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ac532ad..dc919b9 100644 --- a/README.md +++ b/README.md @@ -521,28 +521,11 @@ axios.get('/user/12345', { } }); -// cancel the request (the message parameter is optional) -source.cancel('Operation canceled by the user.'); -``` - -```js -var CancelToken = axios.CancelToken; -var source = CancelToken.source(); - -axios( - method: 'POST', - url: '/user/12345', - data: { - name: 'new name' - }, - cancelToken: source.token -}).catch(function(thrown) { - if (axios.isCancel(thrown)) { - console.log('Request canceled', thrown.message); - } else { - // handle error - } -}); +axios.post('/user/12345', { + name: 'new name' +}, { + cancelToken: source.token +}) // cancel the request (the message parameter is optional) source.cancel('Operation canceled by the user.');