Skip to content

Instantly share code, notes, and snippets.

@shaci
Forked from brenopolanski/vue-axios-cors.md
Created July 13, 2021 16:47
Show Gist options
  • Select an option

  • Save shaci/0b8bcff92d0817d8ed74b74d9bf4d282 to your computer and use it in GitHub Desktop.

Select an option

Save shaci/0b8bcff92d0817d8ed74b74d9bf4d282 to your computer and use it in GitHub Desktop.

Revisions

  1. @brenopolanski brenopolanski revised this gist Sep 29, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vue-axios-cors.md
    Original file line number Diff line number Diff line change
    @@ -101,6 +101,7 @@ Use if u need

    **References**:

    - https://cli.vuejs.org/config/#devserver-proxy
    - http://vuejs-templates.github.io/webpack/proxy.html
    - https://github.com/axios/axios/issues/853#issuecomment-374846375
    - https://stackoverflow.com/questions/40315451/proxy-requests-to-a-separate-backend-server-with-vue-cli?answertab=active#tab-top
  2. @brenopolanski brenopolanski revised this gist Sep 28, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vue-axios-cors.md
    Original file line number Diff line number Diff line change
    @@ -98,6 +98,7 @@ Use if u need
    "changeOrigin": true,
    "secure": false
    ```

    **References**:

    - http://vuejs-templates.github.io/webpack/proxy.html
  3. @brenopolanski brenopolanski revised this gist Sep 28, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions vue-axios-cors.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    **See**: http://vuejs-templates.github.io/webpack/proxy.html

    Proxy requests using Webpack dev server to avoid cors in development mode.

    In your webpack.config file add:
    @@ -100,4 +98,8 @@ Use if u need
    "changeOrigin": true,
    "secure": false
    ```
    **References**:

    - http://vuejs-templates.github.io/webpack/proxy.html
    - https://github.com/axios/axios/issues/853#issuecomment-374846375
    - https://stackoverflow.com/questions/40315451/proxy-requests-to-a-separate-backend-server-with-vue-cli?answertab=active#tab-top
  4. @brenopolanski brenopolanski revised this gist Sep 28, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions vue-axios-cors.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    **See**: http://vuejs-templates.github.io/webpack/proxy.html

    Proxy requests using Webpack dev server to avoid cors in development mode.

    In your webpack.config file add:
  5. @brenopolanski brenopolanski created this gist Sep 28, 2018.
    101 changes: 101 additions & 0 deletions vue-axios-cors.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    Proxy requests using Webpack dev server to avoid cors in development mode.

    In your webpack.config file add:

    ```
    "devServer":{
    "proxy": {
    "/api": {
    "target": 'https://my-target.com',
    "pathRewrite": { '^/api': '' },
    "changeOrigin": true,
    "secure": false
    }
    }
    }
    ```

    The above example will proxy the request
    `axios.post(/api/posts/1 ... . . . .`
    to `https://my-target.com/posts/1`

    The above Git example, u need to change like this in your request:

    ```
    axios.get('/api/a/threads.json', {
    headers: {
    // if u have some headers like this
    // 'Authorization': ' Basic //////some values'
    },
    }).then(function (response) {
    console.log('response is : ' + response.data);
    }).catch(function (error) {
    if (error.response) {
    console.log(error.response.headers);
    }
    else if (error.request) {
    console.log(error.request);
    }
    else {
    console.log(error.message);
    }
    console.log(error.config);
    });
    ```

    In webpack.config file ........

    ```
    "devServer":{
    "proxy": {
    "/api": {
    "target": 'https://a.4cdn.org',
    "pathRewrite": { '^/api': '' },
    "changeOrigin": true,
    "secure": false
    }
    }
    }
    ```

    the modified webpack config proxy will change your request like this..

    ```
    u can see this in chrome dev tool REQUEST HEADERS
    https://a.4cdn.org/a/threads.json
    ```

    u can use multiple paths in devServer proxy like this
    In webpack.config file ........

    ```
    "devServer":{
    "proxy": {
    "/api": {
    "target": 'https://a.4cdn.org',
    "pathRewrite": { '^/api': '' },
    "changeOrigin": true,
    "secure": false
    },
    "/login": {
    "target": 'https://github.com', // for example
    "pathRewrite": { '^/login': '' },
    "changeOrigin": true,
    "secure": false
    },
    "/post": {
    "target": 'https://facebook.com', // for example
    "pathRewrite": { '^/post': '' },
    "changeOrigin": true
    }
    }
    }
    ```

    Use if u need

    ```
    "changeOrigin": true,
    "secure": false
    ```