【发布时间】:2017-03-17 10:25:25
【问题描述】:
我正在前端开发 Reactjs redux,后端开发 Rails api。
所以现在我使用 Fetch api 方法调用 api,但问题是我无法获得可读的错误消息,就像我在网络选项卡中得到的那样
这是我的功能
export function create_user(user,userInfoParams={}) {
return function (dispatch) {
dispatch(update_user(user));
return fetch(deafaultUrl + '/v1/users/',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(userInfoParams)
})
.then(function(response) {
console.log(response);
console.log(response.body);
console.log(response.message);
console.log(response.errors);
console.log(response.json());
dispatch(update_errors(response));
if (response.status >= 400) {
throw new Error("Bad response from server");
}
})
.then(function(json){
console.log("succeed json re");
// We can dispatch many times!
// Here, we update the app state with the results of the API call.
dispatch(update_user(json));
});
}
}
但是当出现错误时,我无法弄清楚如何像查看浏览器网络选项卡时那样获得可读的响应消息
这就是我遇到错误时从网络选项卡中得到的结果。
我的控制台
这是我的 Rails 代码
def create
user = User.new(user_params)
if user.save
#UserMailer.account_activation(user).deliver_now
render json: user, status: 201
else
render json: { errors: user.errors }, status: 422
end
end
但我不知道如何在我的函数中获取它
谢谢!
【问题讨论】:
标签: javascript fetch-api