繁体   English   中英

在 Redux-saga 中使用 Fetch 发布表单数据

[英]Post form data using Fetch in Redux-saga

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))
}

我正在提出发布数据的请求,但我无法做到,这是什么原因?

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

您的问题没有提供足够的细节来提供任何具体的答案,请详细解释您遇到的错误。

我认为你应该用另一个函数包装 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))
}

只是我删除了 Content-Type 并且它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM