如何使用 AXIOS POST 到 reactJS 中的 .php url 端点?

问题描述

使用 axios 调用以 .PHP 结尾的端点时遇到错误请求 400 错误

调用代码如下

const API = {
  userLogin: function () {
    const email = "test@email";
    const password = "Test1234";
    return axios({
      method: "POST",body: {
        email,password,},headers: {
        "Content-Type": "application/json",url: "url ends in /user-login.PHP",});
  },};

电子邮件和密码在我的实际代码中与 URL 不同,但它们作为正文参数提供给我以传递给端点。说明说明使 API 调用方法“POST”并传递电子邮件和密码作为正文参数。

这是我在图片中得到的错误响应。

enter image description here

最大的错误是使用 Fetch,扩展的错误是使用上面的 axios 调用。有什么建议吗?

解决方法

查看 Axios 文档,https://github.com/axios/axios#request-config 您可能希望将代码中 body 部分中的变量作为数据,即 数据: { 电子邮件, 密码, },