代码之家  ›  专栏  ›  技术社区  ›  Jayesh Dhandha

AXIOS删除方法给出403

  •  0
  • Jayesh Dhandha  · 技术社区  · 6 年前

    我正在从我的节点JS应用程序调用delete方法。

    从邮递员那里可以正常工作,但是在调用这个API时给了我403。 来自代码。

    下面是我的示例代码片段:

    const instance = axios.create();
    instance.interceptors.request.use((config) => {
        config.baseURL = 'https://test-dev.com/api/portfolio'
        config.headers = { 'Authorization' : 'Bearer ' + <TOKEN>}
        return config;
    });
    instance.delete('/admin?users=<VALUE>').then(function(response) {
        console.log("Deleted: "+<VALUE>);
    }).catch(function (error) {
        console.log("Deletion failed with error:" + error);
    });
    

    编辑:

    响应(来自Spring Security应用程序):

    无法验证提供的CSRF令牌,因为找不到您的会话

    我以为这已经由AXIOS处理了。

    调用delete方法时,如何在头中传递此值?

    有什么帮助吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   c-chavez    6 年前

    你可以:

    1-使用WithCredentials属性:

    withCredentials: true
    

    所以:

    axios.delete({
        url: 'https://test-dev.com/api/portfolio/admin?users=' + <VALUE>,
        headers: { 'Authorization' : 'Bearer ' + <TOKEN>},
        withCredentials: true
    }).then(function(response) {
        console.log("Deleted: "+<VALUE>);
    }).catch(function (error) {
        console.log("Deletion failed with error:" + error);
    });
    

    xmlhttpRequest.withCredentials属性是一个布尔值, 指示跨站点访问控制请求是否应 使用cookie、授权头或tls等凭据创建 客户端证书。使用凭据设置对 相同的网站请求。

    2-设置CSRF头

    或者:

    headers: {'X-Requested-With': 'XMLHttpRequest',
    'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')}
    

    headers: {'X-Requested-With': 'XMLHttpRequest',
             'X-CSRFToken': 'your token here'}
    

    或者只是:

    headers: {'X-Requested-With': 'XMLHttpRequest'}
    

    3-如果可能,自担风险禁用

    看看 this article