首页 / ACCESS / 解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题
解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1233字,纯文字阅读大概需要2分钟。
内容图文
跨域错误
用axios.post发送请求的时候出现以下错误
解决方法
1 设置Content-Type类型
axios.post(‘url‘,{
data
},{
headers: {‘Content-Type‘: ‘application/x-www-form-urlencoded‘} //加上这个
})
2 改写axios
在<script></script>里增加以下代码
var HTTP = axios.create({
baseURL:‘http://localhost:8081/‘, //这是基础url
headers: {‘Content-Type‘: ‘application/x-www-form-urlencoded‘},
transformRequest: [function (data) {
// Do whatever you want to transform the data
let ret = ‘‘
for (let it in data) {
ret += encodeURIComponent(it) + ‘=‘ + encodeURIComponent(data[it]) + ‘&‘
}
return ret
}]
});
使用:axios.post改为HTTP.post,例如
HTTP.post(‘admin/user/login.action‘,{
name:‘test‘,password:111
})
.then(function(response){
console.log(response.data);
});
备注:这个方式还可以解决用axios发送请求,数据带不过去的问题
解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题
标签:The for script 错误 ret http UNC func fun
本文系统来源:https://www.cnblogs.com/EarlyBridVic/p/12801788.html
内容总结
以上是互联网集市为您收集整理的解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题全部内容,希望文章能够帮你解决解决Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight 跨域问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。