账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Axios 如何发送 FormData 的 POST 请求?
    20
    0

    吾辈测试的时候使用 fetch 调用能够正常执行,但前端使用 Axios 进行对接时却出现了问题。。。

    fetch

    var fd = new FormData()
    fd.append('phone', '12345678910')
    fetch(ctx + '/direct/user/sendCode', {
      method: 'post',
      body: fd,
    })
      .then(res => res.json())
      .then(json => {
        console.log(json)
        register(json.data)
      });

    Axios

    axios.post(ctx + '/direct/user/sendCode', {
      phone: '12345678910'
    })
      .then(res => console.log(res.data))

    但下面的 Axios POST 请求时 headers 中 Content-Type: application/json;charset=UTF-8Fetch 请求时的 headers 中 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary9yC0IqgL9oNvL7nLAxios 应该怎么改呢?

    附:Java 接口大概是这个样子的

    @PostMapping(GlobalConstant.DIRECT_PATH + "/user/sendCode")
    public JsonResult sendCode(User user) {
        return null;
    }

    或者有更好的请求方法呢?╰( ´◔ ω ◔ `)╯

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 回不到の从前 普通会员 1楼

      Axios 是一个 JavaScript 库,用于创建 RESTful API。它支持多种 HTTP 方法,包括 POST。以下是如何使用 Axios 发送 FormData 的 POST 请求的示例:

      javascript axios.post('https://example.com/api/data', formData) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });

      在这个示例中,我们首先使用 Axios 的 post 方法发送一个 POST 请求到 https://example.com/api/data。然后,我们使用 .then 方法来处理响应。如果请求成功,我们打印出响应的数据。如果请求失败,我们打印出错误信息。

      formData 是一个对象,其中包含表单数据。例如:

      javascript let formData = { name: 'John Doe', email: 'john.doe@example.com' };

      你可以将 formData 对象作为参数传递给 axios.post 方法,如下所示:

      javascript axios.post('https://example.com/api/data', formData) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });

      在这个示例中,formData 对象将被传递给 Axios 的 formData 属性。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部