通过uni-app开发移动端软件时,运行到浏览器后出现 H5端跨域问题:
报错如下:
Access to XMLHttpRequest at ‘http://:9001/uap/rest/v2/oauth/token’ from origin 'http://:8088’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: Redirect is not allowed for a preflight request.

请问一下,这是什么原因?
请求是如何发送的?请求方式、header 和 body 都有哪些内容?
你贴的这个错误是,Redirect is not allowed for preflight request. 看来是触发了浏览器发送 OPTIONS 请求,而后台返回 3xx 的重定向code。
请求代码如下:
uni.request({
url:uni.getStorageSync('url')+ '/uap/rest/v2/oauth/token',
method: "POST",
data: {
grant_type: "password",
username: this.phoneData,
password: this.passData
},
withCredentials:true,
header: {
//请求类型必须是 application/x-www-form-urlencoded,编码方式为 UTF-8。
"Content-Type": "application/x-www-form-urlencoded", //自定义请求头信息
Authorization: "Basic Y2xpZW50OnNlY3JldA==" //REST API 客户端 ID 和密码
},
success: (res) => {
this.isRotate = false
uni.setStorageSync('username', this.phoneData)
if (!res.data.error && res.data.access_token) { //&&res.statusCode==200
let token = {
"access_token": res.data.access_token,
"refresh_token": res.data.refresh_token,
} //保存用户信息和accesstoken
uni.setStorageSync("token", token); //缓存token
this.isLogin()
} else if (res.data.error == "invalid_grant") {
uni.showToast({
icon: 'none',
title: '用户名或密码错误!'
})
} else if (res.data.error == "invalid_token") {
console.log("invalid_token")
this.refresh()
} else {
uni.showToast({
icon: 'none',
title: '错误:' + res.data.error
})
}
},
fail: (res) => {
this.isRotate = false
console.log('fail');
uni.showToast({
icon: 'none',
title: '网络错误,请稍后重试'
})
}
});
下面截图是一个成功的请求,比较一下请求头和格式看看:



谢谢您的解答,找到问题所在了,在服务端web配置文件中限制了部分数据请求方法,从而导致无法获取token。