【问题标题】:NuxtJS axios post to cockpit CMSNuxtJS axios 发布到驾驶舱 CMS
【发布时间】:2019-04-22 01:20:53
【问题描述】:

我目前正在构建一个 nuxtjs 项目,其中 cockpit 作为我的无头 CMS。目前我发现使用 axios 提交帖子数据时存在问题。使用以下提取时,它按预期工作:

fetch('/api/forms/submit/Inschrijven?token=xxxtokenxxx', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
       form: {
            author: 'John Doe',
            content: 'Something',
            published: true
        }
    })
});

但遗憾的是,IE 不支持 fetch,IE 是该项目所需的浏览器。因此,我们使用 axios 来处理我们的请求,但在与上述相同的 url 上使用 axios.post 总是返回“找不到路径”。

axios.post('/api/forms/submit/Inschrijven?token=xxtokenxx', {
    author: 'John Doe',
    content: 'Something',
    published: true
})
.then(entry => entry.json())
.then(entry => console.log(entry));

我怀疑 API 有问题,无法将其识别为真正的 json 帖子。这里有人知道为什么 axios.post 不起作用吗?

【问题讨论】:

  • 你在设置 axios baseURL 吗?
  • 是的,这已经完成了!

标签: vue.js axios fetch nuxt.js cockpit-cms


【解决方案1】:

我之前使用过 cockpit cms 和 nuxtjs,我可以说有几处与您提出请求的方式不同。

  1. 您未能通过标头
  2. 使用 JSON.stringify

    axios.post( 'https://something.com/api/forms/submit/contact?token=XXX', JSON.stringify({ form: { name: this.name, model: this.carModel, service: this.service, number: this.models.phoneNumber } }), { headers: { 'Content-Type': 'application/json' } } ) 类似于the docs上的示例

    fetch('/api/forms/submit/cockpitForm?token=xxtokenxx', { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ form: { field1: 'value1', field2: 'value2' } }) }) .then(entry => entry.json()) .then(entry => console.log(entry));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多