大家好,我正在关注此https://marmelab.com/admin-on-rest/index.html,对于登录,我正在关注https://marmelab.com/admin-on-rest/Authentication.html:
import { AUTH_LOGIN } from 'admin-on-rest';
import restClient from './restClient';
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { username, password } = params;
const request = new Request('http://localhost:9000/login', {
method: 'POST',
headers: new Headers({"Content-Type": "application/json"}),
body: JSON.stringify({ username: username,
password: password})
})
return fetch(request)
.then(response => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
return response.json();
})
.then(({ token }) => {
localStorage.setItem('token', token)
});
}
return Promise.resolve();
}
对于 API,我使用的是 Rails 5.0,在运行上面的代码并在 API 端调试参数时,我无法获得参数主体,结果如下:
<ActionController::Parameters {"controller"=>"sessions", "action"=>"create"} permitted: false>
我试图将发送的标头(内容类型)请求更改为:
...
headers: new Headers({"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"}),
...
并在 API 端再次调试参数,结果:
<ActionController::Parameters {"{\"username\":\"jhon\",\"password\":\"dow\"}"=>nil, "controller"=>"sessions", "action"=>"create"} permitted: false>
那么如何使获取参数如下:
ActionController::Parameters {"username"=>"jhon", "password"=>"doe", "controller"=>"sessions", "action"=>"create"} 允许:false>