axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……

报错如下

axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……_前端
报错原因:接口不正确Access-Control-Allow-Headers不允许请求报头字段内容类型。

解决办法:

在服务器端设置一下接口,使用post方法是不需要设置headers里面的Content-Type, 使用了post,Content-Type是什么就根据你传入参数的格式

  • 如果传入的是对象:它就变成application/json的形式
  • 如果传入的是字符串:它就变成application/x-www-form-urlencoded的形式
axios.post("https://www.imooc.com/api/http/json/search/suggest?word=js", {
username: 'Cai',
age: 19
})
.then(response => {
console.log(response);
}).catch(err => {
console.log(err);
})

axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……_ios_02

axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……_json_03