繁体   English   中英

不要在api上设置POST请求,获取406不可接受

[英]Don't set POST request on api, get 406 Not Acceptable

不要在api上设置POST请求,获取406 Not Acceptable。 尝试使用axios并收到406错误。 被认为是axios问题的原因。 然后,我尝试使用fetch()并再次出现406错误。

我认为,不seted头Content-Type ,当我送他-获得400错误的请求,并在标签预览Unauthorized header content-type

需要您的帮助,我在做什么错? 谢谢大家的帮助。

  • 环境:节点v8.9.4,chrome 64.0.3282.119,Ubuntu 16.04
  • axios版本:0.16.2
  • Vue.js 2.4.2
  • vue-axios 2.0.2
  • api平台/ api包:1.0
  • Symfony 4.0.4

标头 审查

第一个示例fetch()代码:

const json = JSON.stringify({
    a: 1,
    b: 2,
});
fetch('http://localhost:8080/api/products', {
    method: 'POST',
    headers: {
         // 'Content-Type': 'application/json',
    },
    body: json,
});

第二个示例Axios代码:

const data = JSON.stringify({
    data: this.cardData.brand,
});
const configAxios = {
    headers: {
     // 'Content-Type': 'application/json',
     },
};
axios.post('api/products', data, configAxios)
.then((res) => {
    this.cardData.brand = res.data;
    console.log(res);
})
.catch((err) => {
    console.warn('error during http call', err);
});

解!

在axios或fetch()代码中取消注释字符串:

headers: {
     'Content-Type': 'application/json',
},

如果同时使用Symfony 4,请设置nelmio/cors-bundle ,运行命令composer req nelmio/cors-bundle 并配置nelmio_cors.yml ,即添加allow_headers

nelmio_cors:
defaults:
    allow_credentials: false
    allow_origin: []
    allow_headers: ['Content-Type']
    allow_methods: []
    expose_headers: []
    max_age: 0
    hosts: []
    origin_regex: false
    forced_allow_origin_value: ~
paths:
    '^/api/':
        allow_origin: ['*']
        allow_headers: ['X-Custom-Auth', 'Content-Type']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600
    '^/':
        origin_regex: true
        allow_origin: ['^http://localhost:[0-9]+']
        allow_headers: ['X-Custom-Auth', 'Content-Type']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600
        hosts: ['^api\.']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM