服务器端脚本
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");`
public function update(){
echo $_POST["brand"];//简单测试
}
使用axios进行get请求时能进行正常访问
POST请求时

Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
我在网上搜索许多,发现大多数说明为跨域问题,但get请求却能正常执行。
最后发现,这个不是跨域问题,这只是axios的post请求参数格式出问题了
解决方法
引用qs处理参数
原先代码
axios.post('http://www.school.com/index.php?p=home&c=AddList&a=update',that.product)
处理后结果
import qs from 'qs';
axios.post('http://www.school.com/index.php?p=home&c=AddList&a=update',qs.stringify(that.product))
成功获取结果

在使用axios进行POST请求时遇到'Request header field content-type is not allowed'问题,该问题并非跨域导致,而是POST请求参数格式设置不正确。通过引入qs库处理参数,将原本的参数格式化,可以成功解决此问题并获取到结果。
4167

被折叠的 条评论
为什么被折叠?



