获取 api post 调用在 React js 中返回 403 禁止错误,但在邮递员中工作的 URL 相同

Man*_*m V 6 reactjs reactjs-flux

下面的代码不起作用并返回 403 forbidden 但相同的 url 提供了正确的响应邮递员工具。

fetch('https://example.com/', {
  method: 'POST',
  headers: {'Content-Type': 'application/json', },
 body:JSON.stringify(sampledata),
}).then(result => console.log('success====:', result))
  .catch(error => console.log('error============:', error));
Run Code Online (Sandbox Code Playgroud)

Pau*_*uli 5

您需要在您的请求中添加凭据:'include'

fetch('https://example.com/', {
      credentials: 'include',
      method: 'POST',
      headers: {'Content-Type': 'application/json', },
      body: JSON.stringify(sampledata),

      }).then(result => console.log('success====:', result))
        .catch(error => console.log('error============:', error));
Run Code Online (Sandbox Code Playgroud)


mra*_*won 2

可能是 CORS 问题。常规网页可以从远程服务器发送和接收数据,但它们受到同源策略的限制。像邮递员这样的扩展则不是。您必须在后端配置 CORS。