React POST请求返回错误422无法处理的实体

时间:2020-05-05 14:49:12

标签: api post react-redux fetch

我使用Rails数据创建了一个API。我想从React添加数据,为此我使用POST请求:

export async function createUserCours(user_cours) {
  console.log(user_cours);
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(user_cours)
  };

  const response = await fetch('/api/v1/cours_users', options);
  console.log(response);
  const data = await response.json();

  return {
    type: 'USER_COURS_CREATED',
    payload: data
  }
}

但是获取请求失败,并返回422状态错误(无法处理的实体)。

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案:可靠的CSRF保护。您可以使用以下命令编辑控制器:

skip_before_action :verify_authenticity_token