【问题标题】:Post form data using Fetch in Redux-saga在 Redux-saga 中使用 Fetch 发布表单数据
【发布时间】:2017-09-23 14:02:43
【问题描述】:
var formData = new FormData(loginRequestObject);
formData.append('userName', loginRequestObject.username);
formData.append('password', loginRequestObject.password);
formData.append('mobile', loginRequestObject.mobile);
formData.append('deviceid', deviceInfo.getDeviceId())

这是我正在提出的要求-

在身体部位

  try{
const response = yield call(fetch, Url, {
  method : 'POST',
  headers : {
    'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
    'Content-Type' :'multipart/form-data, application/x-www-form-urlencoded; charset=utf-8'
  },
  body : formData
}) 
if(response.ok) {
  const jsonResponse = yield response.text()
  yield put(LoginActions.loginSuccess(jsonResponse))
} else {
  const jsonResponse = yield response.text();
  yield put(LoginActions.loginFailure(jsonResponse))
}

我要求发布数据,但我不能这样做,是什么原因?

【问题讨论】:

  • 你的代码看起来没问题,你怎么看到它不起作用?它会发送一个 http 帖子吗?
  • 这个调用函数是关于什么的?为什么不直接使用 ES6 fetch 将 fromData 发布到服务器?如果发生了什么事,你检查过你的网络标签吗?
  • 当我使用发布时它告诉** type == null **。并且在我使用 `body:JSON.stringify(formData)` 时发生网络调用,但值将不正确。

标签: reactjs react-native ecmascript-6 redux-saga


【解决方案1】:

首先,没有promise function 处理您的POST 请求的响应。这个实现的代码有什么错误?当您不分多个部分发送数据时,为什么要使用multipart/form-data?从您的代码来看,您似乎没有发送您需要multipart 的图像数据。

您的问题没有提供足够的详细信息来提供任何具体的答案,请详细说明您遇到的错误。

【讨论】:

  • 添加了 multipart/form-data 因为我得到了不同的 diffrent 数组,如果我要删除它,我会收到 multipart 的错误 !=application/x-www-form -url-enoded'
【解决方案2】:

我认为你应该用另一个函数包装 fetch 函数,这是给你的演示

const fetchFunc = ({ url, options }) => {
   return fetch(url, options);
}

const response = yield call(fetchFunc, {
     url: Url,
     options {
        method : 'POST',
        headers : {
          'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
          'Content-Type' :'multipart/form-data, application/x-www-form-urlencoded;charset=utf-8',
        },
        body : formData
    }
}); 
if(response.ok) {
   const jsonResponse = yield response.text()
   yield put(LoginActions.loginSuccess(jsonResponse))
} else {
   const jsonResponse = yield response.text();
   yield put(LoginActions.loginFailure(jsonResponse))
}

【讨论】:

    【解决方案3】:

    只是我删除了 Content-Type 并且它已经工作了。

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 2017-04-08
      • 2021-12-19
      • 2017-08-27
      • 2017-09-12
      • 2018-10-20
      • 2018-03-20
      • 2022-07-31
      • 1970-01-01
      相关资源
      最近更新 更多