Я хотел бы сделать следующее в JavaScript:выборки возвращает 400 при отправке на локальный
curl --data "client_id=admin-cli&username=xx&password=xx&grant_type=password" http://localhost:8082/auth/realms/mine/protocol/openid-connect/token
А вот моя попытка:
const data = {"client_id":"admin-cli","username":"xx","password":"xx,"grant_type":"password"};
const formData = new FormData();
for(name in data) {
formData.append(name, data[name]);
}
fetch("http://localhost:8082/auth/realms/mine/protocol/openid-connect/token", {
method: "POST",
body: formData
})
.then(function (response) {
return response.text();
})
.then(function (text) {
console.log('Request successful', text.length,text);
})
.catch(function (error) {
console.log('Request failed', error)
});
Которая производит ошибку:
POST http://localhost:8082/auth/realms/mine/protocol/openid-connect/token 400 (Bad Request)
localhost/:1 Fetch API cannot load http://localhost:8082/auth/realms/mine/protocol/openid-connect/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:18080' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
bundle.js:24428 Request failed TypeError: Failed to fetch
Что я делаю неправильно? Я делаю доказательство концепции, поэтому я буду рад любому решению, пока он возвращает мне токен.
Я также попытался:
fetch("http://localhost:8082/auth/realms/mine/protocol/openid-connect/token?client_id=admin-cli&username=sa&password=sa&grant_type=password", {
method: "POST"
})
с тем же результатом.