如何在React + JS中发送带有表单数据的POST请求

时间:2019-08-08 22:13:45

标签: javascript reactjs fetch multipartform-data

在ReactJS中发送POST请求时遇到一些麻烦 我的减速器看起来像这样:

export const addTodo = todo => async (dispatch, getState) => {
  let URL = "https://uxcandy.com/~shapoval/test-task-backend/v2/create/?developer=Nazar";
  const { username, text, email } = todo;
  const data = {
    username: username,
    text: text,
    email: email
  };
  console.log(JSON.stringify(data));
  await fetch(URL, {
    method: "POST",
    headers: { contentType: "application/json" },
    body: JSON.stringify(data)
  })
    .then(response => response.json())
    .then(data => {
      dispatch({ type: "ADD_TODO", payload: data });
    })
    .catch(err => {});
};

我需要像POSTMAN enter image description here

中的图片一样发送POST请求

感谢您的帮助,祝您有美好的一天!

0 个答案:

没有答案