【问题标题】:I am trying to make a post request in a API from React but i get "net::ERR_ABORTED 415"我正在尝试从 React 的 API 中发出发布请求,但我得到“net::ERR_ABORTED 415”
【发布时间】:2021-02-14 21:25:32
【问题描述】:

这里是代码,这里是我连接到“https://hub.graphistry.com/api-token-auth/”的 web api,我尝试使用 axios 但 id 没有帮助科斯

 import React, { Component } from "react";

export default class App extends Component {
  async postData() {
    try {
      let result = await fetch("https://hub.graphistry.com/api-token-auth/", {
        method: "post",
        mode: "no-cors",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          username: "orlando",
          password: "graphistry1234",
        }),
      });

      console.log(result);
    } catch (e) {
      console.log(e);
    }
  }
  render() {
    return (
      <div>
        <button onClick={() => this.postData()}>
          Press me to post some data
        </button>
      </div>
    );
  }
}

【问题讨论】:

  • 请不要将错误信息发布为图片。

标签: reactjs api fetch


【解决方案1】:

在这种情况下,您需要使用 form-data 如下:

let formData = new FormData();
formData.append("username", "orlando");
formData.append("password", "graphistry1234");

fetch("https://hub.graphistry.com/api-token-auth/", {
   method: "POST",
   body: formData
})

fetch() 不期望在 body 中有一个 JavaScript 对象。 curl 命令和 fetch() 模式不一样。

【讨论】:

  • 别提了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 2020-09-12
  • 2021-05-25
  • 1970-01-01
  • 2015-09-23
相关资源
最近更新 更多