我正在尝试使用API并尝试获取它,但它给出了错误。我已经尝试了较旧的解决方案,但没有任何工作 POST https://beenux.my.salesforce.com/services/oauth2/token 400(错误请求)
访问CORS策略已阻止从源“空”获取“ https://beenux.my.salesforce.com/services/oauth2/token”处的访问:请求的资源上不存在“ Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”以在禁用CORS的情况下获取资源。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
async function swagger(){
try {
var formdata = new FormData();
formdata.append("grant_type", "password");
formdata.append("client_id", "client");
formdata.append("client_secret", "client_secret");
formdata.append("username", "username");
formdata.append("password", "password");
const data = await postData('https://beenux.my.salesforce.com/services/oauth2/token',formdata);
console.log(JSON.stringify(data)); // JSON-string from `response.json()` call
} catch (error) {
console.error;
}
async function postData(url = '', data = {}) {
// Default options are marked with *
console.log(data)
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
//'Content-Type': 'application/json'
'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}
}
</script>
</head>
<body>
<button type="button" onclick="swagger()">
Click me</button>
</body>
</html>
答案 0 :(得分:0)
是否已与邮递员确认您的要求?