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

问题描述 投票:0回答:3
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))
}

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

reactjs react-native ecmascript-6 redux-saga
3个回答
0
投票

首先,没有

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

您的问题没有提供足够的详细信息来提供任何具体答案,请用有关您收到的错误的更多详细信息进行解释。


0
投票

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

-2
投票

只是我删除了内容类型,它就起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.