客户端使用 fetch 发送 ajax 请求,一直出现401错误,一直认为是服务器端跨域设置问题,后来发现是客户端发送参数不对,
此方式不对,一直返回401
fetch('http://localhost:8080/oauth/token', { method: 'POST', model: 'cros', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ 'grant_type': 'password', 'scope': 'select', 'client_id': 'client', 'client_secret': 'client-ppp', 'username': 'zhangsan', 'password': '123456' })}) .then(function (response) { console.log('==================1===============') console.log(response) }) .catch(function (error) { console.log('==================2===============') console.log(error) })
下面这种可以正常发送参数
let formData = new FormData()// 请求参数 ('key',value)formData.append('grant_type', 'password')formData.append('scope', 'select')formData.append('client_id', 'client')formData.append('client_secret', 'client-ppp')formData.append('username', 'zhangsan')formData.append('password', '123456')fetch('http://localhost:8080/oauth/token', { method: 'POST', model: 'cros', headers: { 'Accept': 'application/json' }, body: formData}) .then(function (response) { console.log('==================1===============') console.log(response) }) .catch(function (error) { console.log('==================2===============') console.log(error) })