我需要致电以获取令牌。我想使用访存,但仍然收到500错误。 (jquery ajax请求工作正常,但是我正在使用react,我想避免为此安装jquery)。这里有什么区别?
AJAX(工作正常,我得到了令牌)
var settings = {
"async": false,
"crossDomain": true,
"url": "https://api.com/oauth2/token",
"method": "POST",
"headers": {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Postman-Token": "postmantoken"
},
"data": {
"client_id": clientId,
"client_secret": clientSecret,
"grant_type": "password",
"username": username,
"password": password,
}
}
$.ajax(settings).done(function (response) {
aTkn = response.access_token;
});
return aTkn;
获取:得到500错误:{错误:“ invalid_request”,错误说明:“对象引用未设置为对象的实例。”}错误:“ invalid_request”错误说明:“对象引用未设置为对象的实例对象。”
fetch("https://api.com/oauth2/token", {
crossDomain:true,
method: 'post',
headers: { "Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Postman-Token": postmantoken},
body:{
"client_id": clientId,
"client_secret": clientSecreet,
"grant_type": "password",
"username": username,
"password": password,
}
}).then((data)=>{debugger})
答案 0 :(得分:0)
您传递给获取的数据必须进行字符串化处理,否则将传递诸如[object Object]
之类的信息:
fetch("https://api.com/oauth2/token", {
crossDomain: true,
method: 'post',
headers: {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Postman-Token": postmantoken
},
body: JSON.stringify({
"client_id": clientId,
"client_secret": clientSecreet,
"grant_type": "password",
"username": username,
"password": password,
})
}).then((data) => { debugger })
希望这会有所帮助!