【问题标题】:Get a 401 error with POST and PUT Laravel passport/Vue/Axios使用 POST 和 PUT Laravel 护照/Vue/Axios 时出现 401 错误
【发布时间】:2018-11-17 20:29:32
【问题描述】:

我正在开发一个带有单独的 Laravel 后端 API 的 Vue 应用程序。后端有 Laravel 护照,在进行数据库调用时需要访问令牌。

通常一切正常,我可以从数据库中取回数据,但由于某种原因,我的 2 个调用出错,即 POST en PUT。我不知道为什么我从 laravel 护照返回未经身份验证(401),而我的 get 请求进展顺利。此外,POST 和 PUT 在邮递员应用程序中运行良好。

get 请求

   getSuppliers() {
        axios.get(`${this.$API_URL}/api/v1/relations`, {
            headers: this.headers,
        })
            .then((response) => {
                this.loaded = true;
                this.relations = response.data.data;
            })
            .catch(error => console.log(error));
    },

post 请求

    axios.post(`${this.$API_URL}/api/v1/relations`, {
        headers: this.headers,
        data: {
            company_name: this.supplier.company_name,
                    language_id: this.supplier.language_id,
                    supplier_type_id: this.supplier.supplier_type_id,
                    email: this.supplier.email,
                    website: this.supplier.website,
                    recognition_number: this.supplier.recognition_number,
                    street: this.supplier.street,
                    house_number: this.supplier.house_number,
                    zipcode: this.supplier.zipcode,
                    city: this.supplier.city,
                    country: this.supplier.country,
                },
            })
                .then((response) => {
                    console.log(response);
                    // retrieve the id
                    // push the user to the show of the retrieved id
                })
                .catch(error => console.log(error));

访问令牌的辅助函数

function getHeaders(token) {
    return {
        Accept: 'application/json',
        Authorization: `Bearer ${token}`,
    };
}

function getToken() {
    const oauth = JSON.parse(localStorage.getItem('oauth') || '{}');
    if (oauth.access_token) {
        return oauth.access_token;
    }
    return false;
}

有人遇到同样的问题或类似问题吗?

【问题讨论】:

    标签: laravel vue.js axios laravel-passport


    【解决方案1】:

    经过一些挖掘,查看我的其他代码和请求后,我找到了一个解决方案来为我修复它。

    代替

    axios.post(`${this.$API_URL}/api/v1/relations`, {
        headers: this.headers,
        data: {
            company_name: this.supplier.company_name,
                    language_id: this.supplier.language_id,
                    supplier_type_id: this.supplier.supplier_type_id,
                    email: this.supplier.email,
                    website: this.supplier.website,
                    recognition_number: this.supplier.recognition_number,
                    street: this.supplier.street,
                    house_number: this.supplier.house_number,
                    zipcode: this.supplier.zipcode,
                    city: this.supplier.city,
                    country: this.supplier.country,
                },
            })
                .then((response) => {
                    console.log(response);
                    // retrieve the id
                    // push the user to the show of the retrieved id
                })
                .catch(error => console.log(error));
    

    我不得不这样做

    axios({
        method: 'post',
        url: `${this.$API_URL}/api/v1/relations`
        headers: this.headers,
        data: {
            company_name: this.supplier.company_name,
                    language_id: this.supplier.language_id,
                    supplier_type_id: this.supplier.supplier_type_id,
                    email: this.supplier.email,
                    website: this.supplier.website,
                    recognition_number: this.supplier.recognition_number,
                    street: this.supplier.street,
                    house_number: this.supplier.house_number,
                    zipcode: this.supplier.zipcode,
                    city: this.supplier.city,
                    country: this.supplier.country,
                },
            })
                .then((response) => {
                    console.log(response);
                    // retrieve the id
                    // push the user to the show of the retrieved id
                })
                .catch(error => console.log(error));
    

    除了样式我看不出有任何区别,所以我不知道为什么第二种样式有效而第一种无效,尤其是对于 putpost 类型的请求。

    【讨论】:

    • 这里也一样,这怎么可能是合乎逻辑的解释?速记电话不起作用,但普通电话可以......
    猜你喜欢
    • 2019-02-21
    • 2018-05-15
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2020-12-07
    • 2021-07-27
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多