在获取方法期间更改为状态为 200 的选项并成功获取

问题描述 投票:0回答:2

我在 React 中遇到 Fetch 问题,我正在使用这段代码,我应该收到此状态 http://docs.recruitment-api.pyt1.stg.jmr.pl/# 但我收到 成功:{"message":"所请求的 URL 不允许使用该方法。"} 状态 200 正常 方法也更改为 OPTIONS http://prntscr.com/lu0lat

submitToApi = e => {
e.preventDefault();
const url =
  'https://cors-anywhere.herokuapp.com/http://recruitment-api.pyt1.stg.jmr.pl/login';
const data = {
  login: this.state.email,
  password: this.state.password
};
fetch(url, {
  method: 'POST',
  body: JSON.stringify(data),
  headers: {
    'Content-Type': 'application/json'
  }
})
  .then(res => res.json())
  .then(response => console.log('Succes: ', JSON.stringify(response)))
  .catch(error => console.error('Error: ', error));
this.setState({
  email: '',
  password: ''
});

};

reactjs http protocols fetch-api
2个回答
0
投票

您的react/fetch代码没有任何问题。 您绕过了 CORS,但 .pyt1.stg.jmr.pl 上的 api 可能会理解它,并返回带有 {"message":"The method is not allowed for the request URL."}

的响应。

编辑:尝试从 NodeJS 运行 fetch 部分来验证


-1
投票

在后端微服务中,添加标头以允许方法。

Allow-Control-Access-Methods,"*"

例如:在 NodeJs 中。

res.headers("Allow-Control-Access-Methods","*");
© www.soinside.com 2019 - 2024. All rights reserved.