令牌请求在Docker背后键入,curl工作,reactjs不

以下情况:

Keycloak服务器使用docker撰写( http://127.0.0.1:8083 )

当我这样做:

curl --data "grant_type=password&client_id=account&username=dr&password=dr" http://127.0.0.1:8083/auth/realms/xxx/protocol/openid-connect/token 

有用。

然后,我尝试在react.js上执行以下操作:

 axios.post(kc.authServerUrl + "/realms/xxx/protocol/openid-connect/token", { grant_type: 'password', client_id: 'account', username: 'dr', password: 'dr' } ) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); 

它不起作用。 我得到以下内容:

在这里输入图像说明

注意:在keycolak我设置允许来源*还奇怪:我看到用户已经打开会话从IP 172.17.0.1(这是我想由curl引起的)

问题:如何使react.js工作? 问题2:为什么它从curl而不是axios工作?

好的,这似乎工作:

 fetch(kc.authServerUrl + "/realms/xxx/protocol/openid-connect/token", { method: 'POST', mode: 'no-cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', }, body: 'grant_type=password&client_id=account&username=dr&password=dr' }); 
Interesting Posts