通过Fetch API验证登录

时间:2020-02-21 16:53:03

标签: javascript api fetch-api

我正在尝试通过API连接到某些服务,第一步是验证登录到该服务的身份,以下是我当前的代码:

fetch("https://api.connectservice.com/login",
{
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    },
    method: "POST",
    body: {
  "username": "usermn",
  "password": "m$p@ss"
}
}).then(response => {
  if (!response.ok) throw new Error(response.status);
  return response.json();
})

运行上面的代码时出现错误:

(index):1 Uncaught (in promise) TypeError: Failed to fetch

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

以下是我的一个请求示例

const data = {
        email: this.state.email,
        password: this.state.password,
        date
      };
      console.log(date);

      fetch("/login", {
        method: "POST", // or 'PUT'
        headers: {
          Accept: "application/json,",
          "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
      })

尝试传递与此类似的数据