【问题标题】:problem with axios get error 400 bad requestaxios 的问题得到错误 400 错误请求
【发布时间】:2022-01-10 16:31:58
【问题描述】:

我已经被困了很长一段时间,因为当我调用我的 API 来检索数据(因此是 GET 方法)时,我在邮递员上遇到了 400 错误请求错误,我设法检索状态为 200 的数据。但是在我的 axios 中,我传递了参数以及有用的标头。

代码 axios 状态 400 :

async getAssociatedPlant() {                
                this.$axios.$get('lnk/plant/plants', {
                    headers: {
                        "Content-Type": "application/json",
                    },
                    params: {
                        link: "good",
                        plant_id: this.plantActive.id,
                    },
                    
                })
                    .then(response => {
                        console.log(response) 
                    }).catch(error => {
                        console.log(error)
                    });
            },

图片有助于理解:

Postman Headers status 200 OK

Postman Params status 200 OK

Erreur 400 bad request

Request Payload

Request Response

【问题讨论】:

  • 您忘记在您的路线中添加 'api/...'
  • 不,它存在于我的请求中(参见 400 错误请求的图像),它是 .env 中我的 api 的 base_url
  • 能否请您翻译英文回复
  • 您为什么声称请求正文(由于您正在发出 GET 请求而无法存在)是 JSON 文本?
  • 所以问题不在于您的 getAssociatedPlant 方法,它是 End point 表示正在处理请求的服务器,或者您需要确保这是完全相同的您的邮递员正在与之通信的服务器

标签: javascript vue.js axios postman


【解决方案1】:

尝试将 data:{} 添加到您的请求配置中。

async getAssociatedPlant() {
    this.$axios.$get('lnk/plant/plants', {
        headers: {
            "Content-Type": "application/json",
        },
        data:{},
        params: {
            link: "good",
            plant_id: this.plantActive.id,
        },

    })
        .then(response => {
            console.log(response)
        }).catch(error => {
            console.log(error)
        });
}

【讨论】:

  • 为什么会有帮助?
  • @Quentin 如果没有发送数据,则 Axios 会从标头中删除内容类型。这个github线程可能会解释这个github.com/axios/axios/issues/86
  • @AdarshThakur 我测试过也不起作用...
  • 能否分享更详细的请求或 cURL 截图?
  • @AdarshThakur ibb.co/bLG7v02 这还不够吗?
猜你喜欢
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 2021-12-13
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
相关资源
最近更新 更多